Coding - Algo/python
[백준] 2178번:미로 탐색 (python 파이썬)
jainn
2021. 2. 5. 11:10
728x90
문제
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net
소스코드
import sys
input = sys.stdin.readline
def dfs(x, y, cnt):
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
arr[x][y] = 0
while que:
nowx, nowy, cnt = que.pop(0)
for i in range(4):
nx = nowx+dx[i]
ny = nowy+dy[i]
if nx<0 or ny<0 or nx>=n or ny>=m or not arr[nx][ny]:
continue
if nx == n-1 and ny == m-1:
return cnt+1
break
arr[nx][ny] = 0
que.append((nx, ny, cnt+1))
return -1
n, m = map(int, input().split())
arr = [list(map(int, input()[:-1])) for _ in range(n)]
start = (0, 0, 1)
end = (n-1, m-1)
que = []
que.append(start)
print(dfs(start[0],start[1], start[2]))
반응형