【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; }
相关文章推荐
- hdoj 1547 Bubble Shooter(dfs+dfs)
- HDOJ-1547 Bubble Shooter 简单搜索
- HDU 1547 Bubble Shooter BFS
- 杭电 HOJ 1547 Bubble Shooter 解题报告
- hdu 1547 Bubble Shooter(DFS)
- HDU-1547 Bubble Shooter 搜索
- HDU 1547 Bubble Shooter
- 位运算要注意 (对于HDOJ 1547的思考)
- HDU 1547 Bubble Shooter(BFS蔓延模拟)
- hdu 1547 Bubble Shooter(深搜)
- hdu 1547 Bubble Shooter
- HDU 1547 Bubble Shooter(BFS)
- hdoj 1166 敌兵布阵
- HDOJ 1106 排序
- HDOJ 题目5100 Chessboard(数学乱搞)
- hdoj1237简单计算器
- 【HDOJ】1205 -> 吃糖果
- hdoj1241
- HDOJ 1787 GCD Again (欧拉函数)
- hdoj 5734 5742 5744 2016多校2(复现)<数学---思维----贪心>