-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdem_so_phong.cpp
More file actions
48 lines (46 loc) · 1.19 KB
/
dem_so_phong.cpp
File metadata and controls
48 lines (46 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[1005][1005];
bool used[1005][1005];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
int main()
{
cin >> n >> m;
memset(used, false, sizeof(used));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> a[i][j];
}
}
int dem = 0;
queue<pair<int, int>> q;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(a[i][j] == '.' && !used[i][j])
{
dem++;
q.push({i, j});
while (!q.empty())
{
pair<int, int> x = q.front(); q.pop();
for(int k = 0; k < 4; k++)
{
int ii = x.first + dx[k], jj = x.second + dy[k];
if(ii >= 1 && ii <= n && jj >= 1 && jj <= m && a[ii][jj] == '.' && !used[ii][jj])
{
q.push({ii, jj});
used[ii][jj] = true;
}
}
}
}
}
}
cout << dem << endl;
}