-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path적록색약.py
83 lines (54 loc) · 1.66 KB
/
적록색약.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
67
68
69
70
71
72
73
74
75
76
77
78
# https://www.acmicpc.net/problem/10026
n = int(input())
graph = [list(input()) for _ in range(n)]
visited = [[0] * n for _ in range(n)]
visited_blind = [[0] * n for _ in range(n)]
def dfs(a,b):
color = graph[a][b]
stack = []
stack.append((a,b))
visited[a][b] = 1
dx = [1,0,-1,0]
dy = [0,1,0,-1]
while stack:
x,y = stack.pop()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n:
if visited[nx][ny] == 0 and color == graph[nx][ny]:
stack.append((nx,ny))
visited[nx][ny] = 1
def dfs_blind(a,b):
color = graph[a][b]
stack = []
stack.append((a,b))
visited_blind[a][b] = 1
dx = [1,0,-1,0]
dy = [0,1,0,-1]
while stack:
x,y = stack.pop()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n:
if color == 'R' or color == 'G':
if visited_blind[nx][ny] == 0 and (graph[nx][ny] == 'R' or graph[nx][ny] == 'G'):
stack.append((nx,ny))
visited_blind[nx][ny] = 1
elif visited_blind[nx][ny] == 0 and color == graph[nx][ny]:
stack.append((nx,ny))
visited_blind[nx][ny] = 1
cnt = 0
cnt_blind = 0
for i in range(n):
for j in range(n):
if visited[i][j] == 0:
dfs(i,j)
cnt += 1
for i in range(n):
for j in range(n):
if visited_blind[i][j] == 0:
dfs_blind(i,j)
cnt_blind += 1
print(cnt, cnt_blind)