AcWing 1233. 全球变暖
原题链接
简单
作者:
wjie
,
2020-07-01 22:45:16
,
所有人可见
,
阅读 484
#include <iostream>
#include <cstdio>
#include <queue>
#include <set>
using namespace std;
const int N = 1e3 + 5;
char arr[N][N];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, color, st[N][N];
set<int> s;
struct Node{
int x;
int y;
}start;
bool valid(int x, int y)
{
return x >= 0 && x < n && y >= 0 && y < n;
}
bool judge(int x, int y)
{
for (int i = 0; i < 4; ++i)
{
int nx = x + dx[i], ny = y + dy[i];
if (valid(nx, ny) && arr[nx][ny] != '#')
return false;
}
return true;
}
void bfs()
{
queue<Node> q;
q.push(start);
st[start.x][start.y] = color;
while (!q.empty())
{
Node now = q.front();
q.pop();
for (int i = 0; i < 4; ++i)
{
int nx = now.x + dx[i], ny = now.y + dy[i];
if (valid(nx, ny) && arr[nx][ny] == '#' && !st[nx][ny])
{
st[nx][ny] = color;
q.push({nx, ny});
}
}
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (arr[i][j] == '#' && !st[i][j])
{
++color;
start = {i, j};
bfs();
}
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (st[i][j] && judge(i, j))
s.insert(st[i][j]);
}
}
printf("%d", color-s.size());
return 0;
}