题目链接
C:
bfs + 射线
一个闭合路径用bfs就可以搜出来,只不过还要在搜的过程中判断是否包围了某些点,用射线法就好了,想象有一条射线从某个点往一个边界射出去,然后这个点被某条路径包围与否在路径经过其下方的时候改变一次(我是射往下方)
#include
#include
#include
#include
struct node { int x, y, st; node(int x = 0, int y = 0, int st = 0) :x(x),y(y),st(st){} }; int f[20][20][1<<8]; //x y bomb treasure int n, m, object[8][2], cntOfobject, value[8], number[8]; char mp[21][21]; bool valid(int x,int y) { return x >= 0 && x < n && y >= 0 && y < m && mp[x][y] == '.' || mp[x][y] == 'S'; } int get(int x,int y,int ny,int st) { for(int i = 0; i < cntOfobject; i++) { if(x > object[i][0] && (y == object[i][1] && ny > object[i][1] || ny == object[i][1] && y > object[i][1])) st ^= 1 << i; } return st; } int dx[] = {1,0,0,-1}; int dy[] = {0,1,-1,0}; int bfs(int sx,int sy) { std::queue
Q; memset(f,-1,sizeof(f)); Q.push(node(sx, sy, 0)); f[sx][sy][0] = 0; while(!Q.empty()) { node fr = Q.front(); Q.pop(); for(int i = 0; i < 4; i++) { int tx = fr.x + dx[i]; int ty = fr.y + dy[i]; if(valid(tx, ty)) { int newst = get(tx, ty, fr.y, fr.st); if(f[tx][ty][newst] == -1) { f[tx][ty][newst] = f[fr.x][fr.y][fr.st] + 1; // printf(%d %d %d %d ,tx,ty,newst,f[tx][ty][newst]); Q.push(node(tx, ty, newst)); } } } } int ans = 0; for(int i = 0; i < (1<
> j & 1){ if(number[j] != -1) sum += value[number[j]]; else bomb = true; } if(bomb) continue; ans = std::max(ans, sum - f[sx][sy][i]); } return ans; } int main() { scanf(%d%d,&n,&m); int sx, sy, mx = 0; for(int i = 0; i < n; i++) { scanf(%s,mp[i]); for(int j = 0; j < m; j++) { if(mp[i][j] >= '1' && mp[i][j] <= '8' || mp[i][j] == 'B') { object[cntOfobject][0] = i; object[cntOfobject][1] = j; if(mp[i][j] == 'B') { number[cntOfobject] = -1; } else number[cntOfobject] = mp[i][j] - '1', mx = std::max(mx,mp[i][j]-'0'); cntOfobject++; } if(mp[i][j] == 'S') { sx = i; sy = j; } } } for(int i = 0; i < mx; i++) scanf(%d,&value[i]); printf(%d ,bfs(sx, sy)); return 0; }
D:
一棵树,每个节点有颜色,m个询问,问某个节点的子树内出现次数=ki的颜色的种类数
如果能知道一棵子树的所有信息,然后递归回去的时候能合并子树间的信息就可以了
需要维护的信息是子树内每种颜色出现的次数,然后再把次数插入一棵平衡树
采用启发式合并,合并的时候把size 小的往size大的合并
类似的题:
http://acm.hdu.edu.cn/showproblem.php pid=4358
#include
E:坑