#include <iostream>
#include <queue>
#include <cstring>
#define int long long
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
bool a[n][m];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
char c;
cin >> c;
a[i][j] = (c == '.');
}
}
int ans = 0;
bool vis[n][m];
memset(vis, false, sizeof(vis));
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (vis[i][j])
continue;
if (a[i][j])
continue;
queue<pair<int, int>> q;
q.push(make_pair(i, j));
while (!q.empty())
{
pair<int, int> p = q.front();
q.pop();
int x = p.first, y = p.second;
if (vis[x][y])
continue;
vis[x][y] = true;
for (int k = 0; k < 4; ++k)
{
int nx = x + dx[k], ny = y + dy[k];
if (nx < 0 || nx >= n || ny < 0 || ny >= m)
continue;
if (a[nx][ny])
continue;
q.push(make_pair(nx, ny));
}
}
++ans;
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
276 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |