File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ from collections import deque
2+
3+ def BFS (x ,y ):
4+ q .append ((x ,y ))
5+ dx = [- 1 ,0 ,1 ,0 ]
6+ dy = [0 ,1 ,0 ,- 1 ]
7+ visited [x ][y ] = 1
8+ while q :
9+ x , y = q .popleft ()
10+ for d in range (4 ):
11+ nx = x + dx [d ]
12+ ny = y + dy [d ]
13+
14+ # 범위 안에 있고
15+ # 같은 색이고
16+ # 방문 안한 경우
17+ if 0 <= nx < N and 0 <= ny < N and a [nx ][ny ] == a [x ][y ] and not visited [nx ][ny ]:
18+ # 방문 여부 확인 후, 큐에 넣음
19+ visited [nx ][ny ] = 1
20+ q .append ((nx ,ny ))
21+
22+
23+ N = int (input ())
24+ a = [list (input ()) for _ in range (N )]
25+ q = deque ()
26+
27+ visited = [[0 ] * N for _ in range (N )]
28+ cnt1 = 0
29+
30+ # 적록색약 아닌 경우
31+ for i in range (N ):
32+ for j in range (N ):
33+ if not visited [i ][j ]:
34+ BFS (i ,j )
35+ cnt1 += 1
36+
37+ # 적록색약인 경우
38+ for i in range (N ):
39+ for j in range (N ):
40+ if a [i ][j ] == 'G' :
41+ a [i ][j ] = 'R'
42+
43+ visited = [[0 ] * N for _ in range (N )]
44+ cnt2 = 0
45+ for i in range (N ):
46+ for j in range (N ):
47+ if not visited [i ][j ]:
48+ BFS (i ,j )
49+ cnt2 += 1
50+
51+ print (cnt1 , cnt2 )
You can’t perform that action at this time.
0 commit comments