백준
백준 2178 미로 탐색 (파이썬)
chldkato
2020. 2. 27. 16:11
https://www.acmicpc.net/problem/2178
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
def bfs(x, y):
q = [[x, y]]
d[x][y] = 1
while q:
x = q[0][0]
y = q[0][1]
q.pop(0)
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if a[nx][ny] == 1 and d[nx][ny] == 0:
q.append([nx, ny])
d[nx][ny] = d[x][y] + 1
n, m = map(int, input().split())
a = [list(map(int, input())) for _ in range(n)]
d = [[0]*m for _ in range(n)]
bfs(0, 0)
print(d[n-1][m-1])