-
Notifications
You must be signed in to change notification settings - Fork 0
/
10026.cpp
81 lines (70 loc) · 1.49 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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> ans, ans2;
int n, cmp[2];
int dr[4] = { 0, 0, 1, -1 };
int dc[4] = { 1, -1, 0, 0 };
int check[100][100];
char arr[100][100];
int n_dfs(int, int);
int u_dfs(int, int);
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("\n%c", &arr[i][j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!check[i][j]) {
check[i][j] = 1;
ans.push_back(n_dfs(i, j));
}
}
}
memset(check, 0, sizeof(check));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!check[i][j]) {
check[i][j] = 1;
ans2.push_back(u_dfs(i, j));
}
}
}
printf("%d %d", ans.size(), ans2.size());
return 0;
}
int n_dfs(int r, int c) {
int ret = 1;
for (int i = 0; i < 4; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if (0 <= nr && nr < n && 0 <= nc && nc < n && arr[nr][nc] == arr[r][c] && !check[nr][nc]) {
check[nr][nc] = 1;
ret += n_dfs(nr, nc);
}
}
return ret;
}
int u_dfs(int r, int c) {
int ret = 1;
for (int i = 0; i < 4; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if (0 <= nr && nr < n && 0 <= nc && nc < n && !check[nr][nc]) {
if ((arr[r][c] == 'R' && arr[nr][nc] == 'G') || (arr[r][c] == 'G' && arr[nr][nc] == 'R')) {
check[nr][nc] = 1;
ret += u_dfs(nr, nc);
}
else if (arr[r][c] == arr[nr][nc]) {
check[nr][nc] = 1;
ret += u_dfs(nr, nc);
}
}
}
return ret;
}