-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10026.cpp
88 lines (71 loc) · 2.07 KB
/
10026.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int main() {
cin.tie(0);
std::ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<string> v(n);
for(auto &i: v){
cin >> i;
}
vector<vector<bool>> vis(n, vector<bool>(n, false));
queue<tuple<int, int, char>> q;
int ans1 = 0, ans2 = 0;
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(vis[i][j]) {
continue;
}
ans1++;
q.push({i, j, v[i][j]});
vis[i][j] = true;
while(!q.empty()){
auto[r, c, ch] = q.front();
q.pop();
pair<int, int> diff[4] = {{0,1}, {0,-1}, {1,0},{-1,0}};
for(const auto &d: diff){
int rr = r+d.first;
int cc = c+d.second;
if(rr<0||rr>=n||cc<0||cc>=n||vis[rr][cc]||ch!=v[rr][cc])
continue;
vis[rr][cc] = true;
q.push({rr, cc, ch});
}
}
}
}
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(v[i][j] == 'R')
v[i][j] = 'G';
}
}
fill(vis.begin(), vis.end(), vector<bool>(n, false));
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(vis[i][j]) {
continue;
}
ans2++;
q.push({i, j, v[i][j]});
vis[i][j] = true;
while(!q.empty()){
auto[r, c, ch] = q.front();
q.pop();
pair<int, int> diff[4] = {{0,1}, {0,-1}, {1,0},{-1,0}};
for(const auto &d: diff){
int rr = r+d.first;
int cc = c+d.second;
if(rr<0||rr>=n||cc<0||cc>=n||vis[rr][cc]||ch!=v[rr][cc])
continue;
vis[rr][cc] = true;
q.push({rr, cc, ch});
}
}
}
}
cout << ans1 << " " << ans2;
}