【HDOJ】1547 Bubble Shooter

两次BFS,一次扫描关联点。一次扫描可能掉落的情况(即再次扫描所有非爆炸的联通点)。余下未被扫描的点均爆炸。

 #include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <iostream>
using namespace std; #define MAXN 105
char map[MAXN][MAXN];
bool visit[MAXN][MAXN];
int h, w, x, y;
int dir[][] = {-,, ,, -,, ,, ,, ,-, -,-, ,-}; typedef struct node_t {
int x, y;
node_t() {}
node_t(int xx, int yy) {
x = xx; y = yy;
}
} node_t; bool check(int x, int y) {
int ww = w; if ((x&) == )
ww = w-;
if (x< || x>h || y< || y>ww || visit[x][y])
return true;
return false;
} void bfs_Oth(int x, int y) {
int i, j, nx, ny;
queue<node_t> Q; visit[x][y] = true;
Q.push(node_t(x,y)); while (!Q.empty()) {
node_t nd = Q.front();
Q.pop();
j = (nd.x & )<<;
for (i=j; i<j+; ++i) {
nx = nd.x + dir[i][];
ny = nd.y + dir[i][];
if (check(nx, ny) || map[nx][ny]=='E')
continue;
Q.push(node_t(nx, ny));
visit[nx][ny] = true;
}
}
} void bfs() {
int ans = ;
int i, j, nx, ny;
char c = map[x][y];
queue<node_t> Q; memset(visit, false, sizeof(visit));
visit[x][y] = true;
Q.push(node_t(x,y)); while (!Q.empty()) {
node_t nd = Q.front();
Q.pop();
++ans;
j = (nd.x & )<<;
for (i=j; i<j+; ++i) {
nx = nd.x + dir[i][];
ny = nd.y + dir[i][];
if (check(nx, ny) || map[nx][ny]!=c)
continue;
Q.push(node_t(nx, ny));
visit[nx][ny] = true;
}
} if (ans < ) {
printf("0\n");
return ;
} // search the other bubbles
for (j=; map[][j]; ++j) {
if (!visit[][j] && map[][j]!='E') {
bfs_Oth(, j);
}
} for (i=; i<=h; ++i) {
for (j=; map[i][j]; ++j) {
if (!visit[i][j] && map[i][j]!='E')
++ans;
}
}
printf("%d\n", ans);
} int main() {
int i, j;
#ifndef ONLINE_JUDGE
FILE *fin = freopen("data.in", "r", stdin);
#endif
while (scanf("%d%d%d%d", &h,&w,&x,&y)!=EOF) {
for (i=; i<=h; ++i) {
scanf("%s", map[i]+);
}
bfs();
#ifndef ONLINE_JUDGE
fflush(stdout);
#endif
} return ;
}
上一篇:leetcode 169


下一篇:美众议院听证会实录:区块链技术助力解决网络安全问题,或成信任基石重塑现有行业