forked from jokj624/PSTeamNote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path적록색약(dfs).cpp
50 lines (47 loc) · 901 Bytes
/
적록색약(dfs).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
#include <iostream>
using namespace std;
char color[101][101];
bool visit[101][101]={false};
int n;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
void dfs(int p,int q, char c){
visit[p][q] = true;
for(int i=0; i<4; i++){
int nextx = p+dx[i];
int nexty = q+dy[i];
if(nextx >= 0 && nextx < n && nexty >=0 && nexty <n){
if(!visit[nextx][nexty] && c == color[nextx][nexty]){
dfs(nextx, nexty, color[nextx][nexty]);
}
}
}
}
int all(){
int cnt = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(!visit[i][j]){
dfs(i,j,color[i][j]);
cnt++;
}
}
}
return cnt;
}
int main(){
int count1, count2;
cin >> n;
for(int i=0; i<n; i++){
scanf("%s", &color[i]);
}
count1 = all();
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
visit[i][j] = false;
if(color[i][j] == 'G') color[i][j] = 'R';
}
}
count2 = all();
cout << count1 << " " << count2;
}