Skip to content

Commit a0f9220

Browse files
committed
96차 1번 문제 풀이 (참고)
1 parent 63258e3 commit a0f9220

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

live9/test96/문제1/박희경.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,38 @@
33
def solution(board):
44
answer = 0
55

6-
dx = [-1, 1, 0, 0]
7-
dy = [0, 0, -1, 1]
6+
# 상하좌우
7+
dx = [0, 0, -1, 1]
8+
dy = [1, -1, 0, 0]
89

910
n = len(board)
1011
m = len(board[0])
1112

12-
def bfs(x, y):
13-
q = deque([(x, y)])
14-
cnt = 0
13+
def bfs(x, y, move):
14+
q = deque([(x, y, move)])
15+
visited = [[0] * m for _ in range(n)]
16+
visited[x][y] = 1
17+
1518
while q:
16-
x, y = q.popleft()
17-
cnt += 1
19+
x, y, move = q.popleft()
20+
if board[x][y] == 'G':
21+
return move
22+
# 'D' 만날 때까지 이동 (이 부분 참고)
1823
for i in range(4):
19-
nx = x + dx[i]
20-
ny = y + dy[i]
21-
if 0 <= nx < n or 0 <= y < m or board[x][y] == 'D':
22-
if board[nx][ny] == 'G':
23-
return cnt
24-
q.append((nx, ny))
25-
24+
nx, ny = x, y
25+
while 0 <= nx + dx[i] < n and 0 <= ny + dy[i] < m and board[nx + dx[i]][ny + dy[i]] != 'D':
26+
nx += dx[i]
27+
ny += dy[i]
28+
if not visited[nx][ny]:
29+
visited[nx][ny] = 1
30+
q.append((nx, ny, move + 1))
31+
return -1
32+
33+
2634
for i in range(n):
2735
for j in range(m):
2836
if board[i][j] == 'R':
29-
cnt += bfs(i, j)
30-
print(cnt)
37+
return bfs(i, j, 0)
38+
3139

3240
return answer

0 commit comments

Comments
 (0)