-
Notifications
You must be signed in to change notification settings - Fork 0
/
10026.py
66 lines (55 loc) · 1.43 KB
/
10026.py
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
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000)
n = int(input())
graph = [[] for _ in range(n)]
visit = [[False] * n for _ in range(n)]
graph2 = [[] for _ in range(n)]
visit2 = [[False] * n for _ in range(n)]
for i in range(n):
s = input().rstrip()
for j in s:
if j == 'R':
graph[i].append(0)
graph2[i].append(0)
elif j == 'G':
graph[i].append(0)
graph2[i].append(1)
else:
graph[i].append(1)
graph2[i].append(2)
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
def dfs(x, y, visit):
if visit[x][y]:
return
visit[x][y] = True
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
if 0 <= nx < n and 0 <= ny < n:
if visit[nx][ny] == False and graph[nx][ny] == graph[x][y]:
dfs(nx, ny, visit)
def dfs2(x, y, visit):
if visit[x][y]:
return
visit[x][y] = True
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
if 0 <= nx < n and 0 <= ny < n:
if visit[nx][ny] == False and graph2[nx][ny] == graph2[x][y]:
dfs2(nx, ny, visit)
cnt = 0
cnt2 = 0
for i in range(n):
for j in range(n):
if visit[i][j] == False:
dfs(i, j, visit)
cnt += 1
for i in range(n):
for j in range(n):
if visit2[i][j] == False:
dfs2(i, j, visit2)
cnt2 += 1
print(cnt2, cnt)