Skip to content

Commit c7aca19

Browse files
author
hangyeol
committed
96차 1번 문제 풀이
1 parent 78afe04 commit c7aca19

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

live9/test96/문제1/백한결.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from collections import deque
2+
3+
4+
def solution(board):
5+
map = [list(row) for row in board]
6+
7+
G = (0, 0)
8+
R = (0, 0)
9+
10+
for i in range(len(map)):
11+
for j in range(len(map[0])):
12+
if map[i][j] == 'R':
13+
R = (i, j)
14+
elif map[i][j] == 'G':
15+
G = (i, j)
16+
17+
return bfs(map, G, R)
18+
19+
20+
def bfs(map, G, R):
21+
queue = deque([(R[0], R[1], 0)])
22+
23+
visited = [[False] * len(map[0]) for _ in range(len(map))]
24+
visited[R[0]][R[1]] = True
25+
26+
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
27+
28+
while queue:
29+
x, y, move = queue.popleft()
30+
31+
if (x, y) == G:
32+
return move
33+
34+
for dx, dy in directions:
35+
nx, ny = x, y
36+
while 0 <= nx + dx < len(map) and 0 <= ny + dy < len(map[0]) and map[nx + dx][ny + dy] != 'D':
37+
nx += dx
38+
ny += dy
39+
40+
if not visited[nx][ny]:
41+
visited[nx][ny] = True
42+
queue.append((nx, ny, move + 1))
43+
44+
return -1

0 commit comments

Comments
 (0)