【HDOJ】3316 Mine sweeping

时间:2021-03-19 19:45:11

简单BFS。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; #define MAXN 105 typedef struct node_t {
int x, y;
node_t() {}
node_t(int xx, int yy) {
x = xx; y = yy;
}
} node_t; char map[MAXN][MAXN];
char ans[MAXN][MAXN];
bool visit[MAXN][MAXN];
int dir[][] = {
{-,},{,},{,},{,-},
{,},{,-},{-,},{-,-}
};
node_t s;
int n; bool check(int x, int y) {
return x< || x>=n || y< || y>=n;
} void bfs() {
int i, j, k, tmp;
int x, y, v;
int xx[], yy[], m;
queue<node_t> Q;
node_t nd; memset(visit, false, sizeof(visit));
memset(ans, '.', sizeof(ans));
visit[s.x][s.y] = true;
for (i=; i<n; ++i)
ans[i][n] = '\0';
Q.push(s); while (!Q.empty()) {
nd = Q.front();
Q.pop();
v = m = ;
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (check(x, y))
continue;
xx[m] = x;
yy[m] = y;
++m;
if (map[x][y] == 'X')
++v;
}
ans[nd.x][nd.y] = v+'';
if (v == ) {
for (i=; i<m; ++i) {
if (!visit[xx[i]][yy[i]]) {
visit[xx[i]][yy[i]] = true;
Q.push(node_t(xx[i], yy[i]));
}
}
}
}
} int main() {
int i, j; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d", &n) != EOF) {
for (i=; i<n; ++i)
scanf("%s", map[i]);
scanf("%d %d", &s.x, &s.y); if (map[s.x][s.y] == 'X') {
printf("it is a beiju!\n\n");
} else {
bfs();
for (i=; i<n; ++i)
printf("%s\n", ans[i]);
printf("\n");
}
} return ;
}