Skip to content

Commit

Permalink
add BJ_2178
Browse files Browse the repository at this point in the history
  • Loading branch information
ByeongJun Kim committed Nov 1, 2023
1 parent 0635ce7 commit 4869896
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions 0x09/BJ_2178.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 미로탐색

from sys import stdin
from collections import deque

input = stdin.readline


def bfs(graph, n, m):
queue = deque([(0, 0)])

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

while queue:
x, y = queue.popleft()

for i in range(4):
nx = x + dx[i]
ny = y + dy[i]

if nx < 0 or nx >= n or ny < 0 or ny >= m:
continue
if graph[nx][ny] == 0:
continue

if graph[nx][ny] == 1:
queue.append((nx, ny))
graph[nx][ny] = graph[x][y] + 1
return graph[n - 1][m - 1]


n, m = map(int, input().rstrip().split())

graph = []
for _ in range(n):
tmp = list(map(int, input().rstrip().split()))
graph.append(tmp)

print(bfs(graph, n, m))

0 comments on commit 4869896

Please sign in to comment.