您的位置:首页 > 其它

【HDOJ】1547 Bubble Shooter

2014-10-13 19:05 288 查看
两次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[8][2] = {-1,1, 1,1, -1,0, 0,1, 1,0, 0,-1, -1,-1, 1,-1};

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&1) == 0)
ww = w-1;
if (x<1 || x>h || y<1 || 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 & 1)<<1;
for (i=j; i<j+6; ++i) {
nx = nd.x + dir[i][0];
ny = nd.y + dir[i][1];
if (check(nx, ny) || map[nx][ny]=='E')
continue;
Q.push(node_t(nx, ny));
visit[nx][ny] = true;
}
}
}

void bfs() {
int ans = 0;
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 & 1)<<1;
for (i=j; i<j+6; ++i) {
nx = nd.x + dir[i][0];
ny = nd.y + dir[i][1];
if (check(nx, ny) || map[nx][ny]!=c)
continue;
Q.push(node_t(nx, ny));
visit[nx][ny] = true;
}
}

if (ans < 3) {
printf("0\n");
return ;
}

// search the other bubbles
for (j=1; map[1][j]; ++j) {
if (!visit[1][j] && map[1][j]!='E') {
bfs_Oth(1, j);
}
}

for (i=1; i<=h; ++i) {
for (j=1; 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=1; i<=h; ++i) {
scanf("%s", map[i]+1);
}
bfs();
#ifndef ONLINE_JUDGE
fflush(stdout);
#endif
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: