-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10026.cpp
72 lines (56 loc) · 1.25 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
#include <stdio.h>
char graph[105][105];
char graph2[105][105];
int count = 0;
int count2 = 0;
void dfs(char ch, int x, int y){
if(graph[x][y] != ch || x < 0 || y < 0){
if(graph[x][y] != 'a') count++;
return;
}
graph[x][y] = 'a';
if(graph[x+1][y] == ch) dfs(ch, x+1, y);
if(graph[x-1][y] == ch) dfs(ch, x-1, y);
if(graph[x][y+1] == ch) dfs(ch, x, y+1);
if(graph[x][y-1] == ch) dfs(ch, x, y-1);
return;
}
void dfs2(char ch, int x, int y){
if(graph2[x][y] != ch || x < 0 || y < 0){
if(graph2[x][y] != 'a') count++;
return;
}
graph2[x][y] = 'a';
if(graph2[x+1][y] == ch) dfs2(ch, x+1, y);
if(graph2[x-1][y] == ch) dfs2(ch, x-1, y);
if(graph2[x][y+1] == ch) dfs2(ch, x, y+1);
if(graph2[x][y-1] == ch) dfs2(ch, x, y-1);
return;
}
int main(){
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%s", graph[i]);
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(graph[i][j] == 'G') graph2[i][j] = 'R';
else graph2[i][j] = graph[i][j];
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(graph[i][j] != 'a'){
count++;
dfs(graph[i][j], i, j);
}
if(graph2[i][j] != 'a'){
count2++;
dfs2(graph2[i][j], i, j);
}
}
}
printf("%d %d\n", count, count2);
return 0;
}