-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10026.cpp
More file actions
77 lines (67 loc) · 1.71 KB
/
10026.cpp
File metadata and controls
77 lines (67 loc) · 1.71 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
#include <iostream>
#include <algorithm>
#include <queue>
#include <memory.h>
using namespace std;
int n;
int arr[101][101];
bool visited[101][101];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void BFS(int startX, int startY){
queue<pair<int, int> > Q;
Q.push(make_pair(startX, startY));
visited[startX][startY] = true;
char c = arr[startX][startY];
while(!Q.empty()){
int currX = Q.front().first;
int currY = Q.front().second;
Q.pop();
for(int i = 0; i < 4; i++){
int nextX = currX + dx[i];
int nextY = currY + dy[i];
if(nextX >= 0 && nextX < n && nextY >= 0 && nextY < n && !visited[nextX][nextY] && arr[nextX][nextY] == c){
Q.push(make_pair(nextX, nextY));
visited[nextX][nextY] = true;
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++){
string str;
cin >> str;
for(int j = 0; j < str.size(); j++){
arr[i][j] = str[j];
}
}
int ans1 = 0, ans2 = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(!visited[i][j]){
BFS(i, j);
ans1++;
}
}
}
memset(visited, false, sizeof(visited));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(arr[i][j] == 'G') arr[i][j] = 'R';
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(!visited[i][j]){
BFS(i, j);
ans2++;
}
}
}
cout << ans1 << " " << ans2 << endl;
return 0;
}