-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabarinth.cpp
66 lines (52 loc) · 1.38 KB
/
Labarinth.cpp
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
There is a labyrinth on a matrix
n
×
m
. Each cell is either empty (denoted by a dot), or contains an obstacle (denoted by an asterisk).
A robot can only move through empty cells, and can move only to any of four cells that share an edge with current one.
Print the total number of connected components of empty cells and their sizes, in non-decreasing order.
#include<iostream>
#include<algorithm>
#include<vector>
#include<sstream>
using namespace std;
#define WH 0
#define GR 1
#define BL 2
#define pb push_back
int n, m;
int dfs(int i, int j, vector<string>& s, vector<vector<bool> >& v){
if(i < 0 || i >= n || j < 0 || j >= m || s[i][j] == '*' || v[i][j]) return 0;
int ans = 1;
v[i][j] = true;
ans += dfs(i - 1, j,s,v);
ans += dfs(i + 1, j, s, v);
ans += dfs(i, j - 1, s, v);
ans += dfs(i, j + 1, s, v);
return ans;
}
int main()
{
cin >> n >> m;
vector<string> s(n);
vector<vector<bool> > v(n, vector<bool>(m, 0));
for(int i = 0; i < n; i++){
cin >> s[i];
}
vector<int> cc;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(!v[i][j]){
int a = dfs(i, j, s, v);
if (a) cc.pb(a);
}
}
}
sort(cc.begin(), cc.end());
cout << cc.size() << endl;
for(int x : cc){
cout << x << " ";
}
cout << endl;
return 0;
}