P1365 - [蓝桥杯2018初赛]全球变暖 - New Online Judgehttp://oj.ecustacm.cn/problem.php?id=1365
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
char g[1024][1024] = { 0 };
int vis[1024][1024] = { 0 };
int res = 0;
int cover;
void dfs(int i, int j) {
if (i<1 || j<1 || i>N || j>N) {
return;
}
if (g[i][j] == '.' || vis[i][j]) {
return;
}
vis[i][j] = 1;
res++;
if (g[i + 1][j] == '#' && g[i][j + 1] == '#' && g[i - 1][j] == '#' && g[i][j - 1] == '#') {
cover = 0;
}
dfs(i + 1, j);
dfs(i - 1, j);
dfs(i, j + 1);
dfs(i, j - 1);
}
int main() {
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
cin >> g[i][j];
}
}
int ans = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
res = 0;
cover = 1;
dfs(i, j);
if (res != 0 && cover) {
ans++;
}
}
}
cout << ans;
return 0;
}