1+ import sys
2+ input = sys .stdin .readline
3+ from collections import deque
4+
5+ N = int (input ().strip ())
6+ grid = [list (input ().strip ()) for _ in range (N )]
7+
8+ # 방향 벡터
9+ dx = [- 1 , 1 , 0 , 0 ]
10+ dy = [0 , 0 , - 1 , 1 ]
11+
12+ def bfs (x , y , color , visited , grid ):
13+ queue = deque ([(x , y )])
14+ visited [x ][y ] = True
15+
16+ while queue :
17+ x , y = queue .popleft ()
18+ for i in range (4 ):
19+ nx , ny = x + dx [i ], y + dy [i ]
20+ if 0 <= nx < N and 0 <= ny < N and not visited [nx ][ny ]:
21+ if grid [nx ][ny ] == color :
22+ visited [nx ][ny ] = True
23+ queue .append ((nx , ny ))
24+
25+ def count_regions (grid , is_color_blind ):
26+ visited = [[False ] * N for _ in range (N )]
27+ count = 0
28+
29+ for i in range (N ):
30+ for j in range (N ):
31+ if not visited [i ][j ]: # 방문하지 않은 경우 BFS 탐색 시작
32+ count += 1
33+ color = grid [i ][j ]
34+
35+ # 적록색약 모드라면 R과 G를 동일하게 처리
36+ if is_color_blind and color in "RG" :
37+ bfs (i , j , "R" , visited , grid )
38+ else :
39+ bfs (i , j , color , visited , grid )
40+
41+ return count
42+
43+ # 적록색약이 아닌 경우
44+ normal_count = count_regions (grid , is_color_blind = False )
45+
46+ # 적록색약인 경우 (R과 G를 동일하게 처리한 grid 생성)
47+ for i in range (N ):
48+ for j in range (N ):
49+ if grid [i ][j ] == 'G' :
50+ grid [i ][j ] = 'R'
51+
52+ color_blind_count = count_regions (grid , is_color_blind = True )
53+
54+ print (normal_count , color_blind_count )
0 commit comments