-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10026.py
More file actions
58 lines (43 loc) · 1.26 KB
/
10026.py
File metadata and controls
58 lines (43 loc) · 1.26 KB
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
import sys
sys.setrecursionlimit(10**6)
n = int(input())
paint = [[] for _ in range(n)]
for i in range(n):
paint[i] = list(input())
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
visited = [[False] * n for _ in range(n)]
normalNum = 0
def dfs(sx, sy, color):
visited[sx][sy] = True
for i in range(4):
nx = sx + dx[i]
ny = sy + dy[i]
if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny]:
if paint[nx][ny] == color:
dfs(nx, ny, color)
return
visited2 = [[False] * n for _ in range(n)]
redGreenNum = 0
def dfs2(sx, sy, color):
visited2[sx][sy] = True
for i in range(4):
nx = sx + dx[i]
ny = sy + dy[i]
if 0 <= nx < n and 0 <= ny < n and not visited2[nx][ny]:
if color == 'R' or color == 'G':
if paint[nx][ny] == "R" or paint[nx][ny] == "G":
dfs2(nx, ny, color)
else:
if paint[nx][ny] == color:
dfs2(nx, ny, color)
return
for i in range(n):
for j in range(n):
if not visited[i][j]:
dfs(i, j, paint[i][j])
normalNum += 1
if not visited2[i][j]:
dfs2(i, j, paint[i][j])
redGreenNum += 1
print(f"{normalNum} {redGreenNum}")