@@ -26,25 +26,33 @@ def solve():
2626 visited [r ][c ] = True
2727
2828 candidates = []
29- if dt [robot_sr ][robot_sc ] > 0 :
30- candidates .append ([0 , robot_sr , robot_sc ])
31-
3229 queue = deque ([[0 , robot_sr , robot_sc ]])
33- visited [robot_sr ][robot_sc ] = True
3430
3531 while queue :
36- depth , sr , sc = queue .popleft ()
37- drc = [[0 , 1 ], [1 , 0 ], [0 , - 1 ], [- 1 , 0 ]]
32+ queue_next = deque ()
33+
34+ while queue :
35+ depth , sr , sc = queue .popleft ()
36+
37+ if visited [sr ][sc ] == False : # 인접한 칸에서오는 중복 방문 제거
38+ if dt [sr ][sc ] > 0 :
39+ candidates .append ([0 , sr , sc ])
3840
39- for dr , dc in drc :
40- nr , nc = sr + dr , sc + dc
41- if 0 <= nr < N and 0 <= nc < N and visited [nr ][nc ] == False :
42- if dt [nr ][nc ] == 0 : # 빈칸이면
43- queue .append ([depth + 1 , nr , nc ])
44- elif dt [nr ][nc ] > 0 : # 먼지가 있으면
45- candidates .append ([depth + 1 , nr , nc ])
41+ visited [sr ][sc ] = True
42+ queue_next .append ([depth , sr , sc ])
4643
47- visited [nr ][nc ] = True
44+ if len (candidates ) > 0 :
45+ break
46+
47+ drc = [[0 , 1 ], [1 , 0 ], [0 , - 1 ], [- 1 , 0 ]]
48+
49+ while queue_next :
50+ depth , sr , sc = queue_next .popleft ()
51+ for dr , dc in drc :
52+ nr , nc = sr + dr , sc + dc
53+ if 0 <= nr < N and 0 <= nc < N and visited [nr ][nc ] == False :
54+ if dt [nr ][nc ] >= 0 : # 빈칸이거나 먼지가 있거나
55+ queue .append ([depth + 1 , nr , nc ])
4856
4957 if len (candidates ) == 0 :
5058 robots_next .append ([robot_sr , robot_sc ])
0 commit comments