AcWing 1365. New Online Judge 1365: [蓝桥杯2018初赛]全球变暖
原题链接
简单
作者:
史一帆
,
2021-03-31 20:35:19
,
所有人可见
,
阅读 585
DFS
#include <iostream>
using namespace std;
const int N = 1010;
int n;
char a[N][N];
int vis[N][N] = {0};
const int dx[] = {0, -1, 0, 1};
const int dy[] = {-1, 0, 1, 0};
int flag;
void dfs(int x, int y)
{
vis[x][y] = 1;
if (a[x][y + 1] == '#' && a[x][y - 1] == '#' && a[x + 1][y] == '#' && a[x - 1][y] == '#') flag = 1;
for (int i = 0; i < 4; i ++)
{
int nx = x + dx[i], ny = y + dy[i];
if (vis[nx][ny] == 0 && a[nx][ny] == '#') dfs(nx, ny);
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
cin >> a[i][j];
int ans = 0;
for (int i = 1;i <= n; i ++)
for (int j = 1; j <= n; j ++)
if (a[i][j] == '#' && vis[i][j] == 0)
{
flag = 0;
dfs(i, j);
if (flag == 0) ans ++;
}
cout << ans << endl;
return 0;
}