-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10026.cpp
94 lines (85 loc) · 2.29 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
89
90
91
92
93
94
#include<iostream>
#include<string>
#include<queue>
using namespace std;
bool visited[101][101] = {0,};
char originalMap[101][101] = {0, };
char colorWeeknesMap[101][101] = {0, };
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int answer[2] = {0, };
int n;
queue<pair<int, int>>q;
void bfs1(int y, int x, char target);
void bfs2(int y, int x, char target);
int main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string tmp;
//입력
cin >> n;
for(int i = 1; i <= n; i++){
cin >> tmp;
for(int j = 1; j <= n; j++){
originalMap[i][j] = tmp[j-1];
if(tmp[j-1] == 'G')
colorWeeknesMap[i][j] = 'R';
else colorWeeknesMap[i][j] = tmp[j-1];
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++)
if(visited[i][j] == 0){
bfs1(i, j, originalMap[i][j]);
answer[0]++;
}
}
//방문 초기화
for(int i = 1; i<=n; i++){
for(int j = 1; j <= n; j++)
visited[i][j] = 0;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++)
if(visited[i][j] == 0){
bfs2(i, j, colorWeeknesMap[i][j]);
answer[1]++;
}
}
cout << answer[0] << ' ' << answer[1];
}
void bfs1(int y, int x, char target){
visited[y][x] = 1;
q.push(make_pair(y, x));
while(!q.empty()){
for(int i = 0; i < 4; i++){
int X = q.front().second + dx[i];
int Y = q.front().first + dy[i];
if(X >= 1&& X<=n && Y >=1 && Y<=n){
if(visited[Y][X] == 0 && target == originalMap[Y][X]){
visited[Y][X] = 1;
q.push(make_pair(Y, X));
}
}
}
q.pop();
}
}
void bfs2(int y, int x, char target){
visited[y][x] = 1;
q.push(make_pair(y, x));
while(!q.empty()){
for(int i = 0; i < 4; i++){
int X = q.front().second + dx[i];
int Y = q.front().first + dy[i];
if(X >= 1&& X<=n && Y >=1 && Y<=n){
if(visited[Y][X] == 0 && target == colorWeeknesMap[Y][X]){
visited[Y][X] = 1;
q.push(make_pair(Y, X));
}
}
}
q.pop();
}
}