-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_Apartments_II.cpp
More file actions
80 lines (67 loc) · 1.65 KB
/
Copy pathCount_Apartments_II.cpp
File metadata and controls
80 lines (67 loc) · 1.65 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
int n, m;
int mat[N][N];
int visited[N][N];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
bool isValid(int x, int y){
return !visited[x][y] && mat[x][y] != -1 && x >= 0 && x < n && y >= 0 && y < m;
}
void bfs(int i, int j,int &countRoom){
queue<pair<int, int>> q;
q.push({i,j});
visited[i][j] = true;
while(!q.empty()){
auto par = q.front();
q.pop();
int x = par.first;
int y = par.second;
for(int i = 0; i < 4; i++){
int new_x = x + dx[i];
int new_y = y + dy[i];
if(isValid(new_x, new_y))
{
q.push({new_x, new_y});
visited[new_x][new_y] = true;
countRoom++;
}
}
}
}
int main () {
pair<int, int> dest;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
for (int j = 0; j < m; j++)
{
if(s[j] == '#') mat[i][j] = -1;
else mat[i][j] = 0;
visited[i][j] = false;
}
}
vector<int> countApartment;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (!visited[i][j] && mat[i][j] == 0) {
int countRoom = 1;
bfs(i, j,countRoom);
countApartment.push_back(countRoom);
}
}
}
sort(countApartment.begin(),countApartment.end());
if(!countApartment.empty()){
for (int val:countApartment) {
cout << val << " ";
}
}
else cout << "0";
return 0;
}