思路
遍历一下地图,对所有的四周都无雷的格子进行bfs
做完后再遍历一下地图,补加上剩下的没有被bfs扫描到的格子(这些格子四周必有雷)
C++ 代码
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
char map[305][305];
bool vis[305][305];
int dx[] = { -1,-1,-1,0,0,1,1,1 }, dy[] = { -1,0,1,-1,1,-1,0,1 };
void bfs(int x, int y, int n)
{
queue<pair<int, int>> Q;
Q.push({ x,y });
vis[x][y] = 1;
bool boom;
int a, b;
while (Q.size()) {
auto top = Q.front();
Q.pop();
boom = 0;
for (int k = 0; k < 8; k++) {
a = top.first + dx[k], b = top.second + dy[k];
if (a < 0 || a >= n || b < 0 || b >= n || vis[a][b]) continue;
if (map[a][b] == '*') {
boom = 1;
break;
}
}
if (!boom) {
for (int i = 0; i < 8; i++) {
a = top.first + dx[i], b = top.second + dy[i];
if (a < 0 || a >= n || b < 0 || b >= n || vis[a][b]) continue;
vis[a][b] = 1;
Q.push({ a,b });
}
}
}
}
int solve(int n)
{
int a, b, res = 0;
bool flag;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j] == '*' || vis[i][j]) continue;
flag = 0;
for (int k = 0; k < 8; k++) {
a = i + dx[k], b = j + dy[k];
if (a < 0 || a >= n || b < 0 || b >= n || vis[a][b]) continue;
if (map[a][b] == '*') {
flag = 1;
break;
}
}
if (!flag) {
bfs(i, j, n);
res++;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j] == '.' && !vis[i][j]) res++;
}
}
return res;
}
int main()
{
int T, N;
cin >> T;
for (int i = 1; i <= T; i++) {
cin >> N;
memset(vis, 0, sizeof(vis));
for (int j = 0; j < N; j++) cin >> map[j];
cout << "Case #" << i << ": " << solve(N) << endl;
}
return 0;
}