File tree Expand file tree Collapse file tree 2 files changed +30
-39
lines changed
Expand file tree Collapse file tree 2 files changed +30
-39
lines changed Original file line number Diff line number Diff line change 44
55### 성능 요약
66
7- 메모리: 115528 KB, 시간: 156 ms
7+ 메모리: 115152 KB, 시간: 148 ms
88
99### 분류
1010
1111너비 우선 탐색, 깊이 우선 탐색, 그래프 이론, 그래프 탐색
1212
1313### 제출 일자
1414
15- 2025년 3월 20일 12:05:07
15+ 2025년 3월 29일 12:08:57
1616
1717### 문제 설명
1818
Original file line number Diff line number Diff line change 11from collections import deque
22
3- N = int (input ())
4- RGB = [list (map (str , input ())) for _ in range (N )]
5-
6- dx = [- 1 , 1 , 0 , 0 ]
7- dy = [0 , 0 , - 1 , 1 ]
8-
9- visited = [[False ] * N for _ in range (N )]
103
11- def bfs1 (i , j ):
4+ def bfs (i , j , A ):
125 q = deque ([(i , j )])
136
147 while q :
158 x , y = q .popleft ()
16- for d in range (4 ):
17- nx = x + dx [d ]
18- ny = y + dy [d ]
19- if 0 <= nx < N and 0 <= ny < N and not visited [nx ][ny ] and RGB [nx ][ny ] == RGB [x ][y ]:
9+ for i in range (4 ):
10+ nx , ny = x + dx [i ], y + dy [i ]
11+ if 0 <= nx < N and 0 <= ny < N and not visited [nx ][ny ] and A [nx ][ny ] == A [x ][y ]:
2012 visited [nx ][ny ] = True
21- q .append ((nx , ny ))
13+ q .append ((nx , ny ))
2214
23- def bfs2 (i , j ):
24- q = deque ([(i , j )])
15+ N = int (input ())
2516
26- while q :
27- x , y = q .popleft ()
28- visited [x ][y ] = True
29- for d in range (4 ):
30- nx = x + dx [d ]
31- ny = y + dy [d ]
32- if 0 <= nx < N and 0 <= ny < N and not visited [nx ][ny ]:
33- if RGB [x ][y ] != 'B' and RGB [nx ][ny ] != 'B' :
34- visited [nx ][ny ] = True
35- q .append ((nx , ny ))
36- elif RGB [x ][y ] == 'B' and RGB [nx ][ny ] == 'B' :
37- visited [nx ][ny ] = True
38- q .append ((nx , ny ))
39-
40- ans1 = 0
41- ans2 = 0
17+ change_normal = {'R' : 1 , 'G' : 2 , 'B' : 3 }
18+ change_blind = {'R' : 1 , 'G' : 1 , 'B' : 2 }
4219
20+ first = [list (map (str , input ())) for _ in range (N )]
21+
22+ normal = [[0 ] * N for _ in range (N )]
23+ blind = [[0 ] * N for _ in range (N )]
4324for i in range (N ):
4425 for j in range (N ):
45- if not visited [i ][j ]:
46- bfs1 (i , j )
47- ans1 += 1
26+ normal [i ][j ] = change_normal [first [i ][j ]]
27+ blind [i ][j ] = change_blind [first [i ][j ]]
4828
4929visited = [[False ] * N for _ in range (N )]
30+ dx = [- 1 , 1 , 0 , 0 ]
31+ dy = [0 , 0 , - 1 , 1 ]
32+
33+ ans = [0 , 0 ]
5034
5135for i in range (N ):
5236 for j in range (N ):
5337 if not visited [i ][j ]:
54- bfs2 (i , j )
55- ans2 += 1
38+ bfs (i , j , normal )
39+ ans [0 ] += 1
40+
41+ visited = [[False ] * N for _ in range (N )]
42+ for i in range (N ):
43+ for j in range (N ):
44+ if not visited [i ][j ]:
45+ bfs (i , j , blind )
46+ ans [1 ] += 1
5647
57- print (ans1 , ans2 )
48+ print (* ans )
You can’t perform that action at this time.
0 commit comments