t_미로탈출_152

source github.com/ndb796/python-for-coding-test
type 📌 개발노트
topics 600-알고리즘 & 코딩테스트 613 BFS & DFS
types 문제풀이

문제

n * m 미로
start 1,1 // 출구 n,m
괴물 o : 0 , 괴물 x : 1
최소 개수, 시작칸과 마지막 칸 포함

common한 bfs문제였다.

import sys
from collections import deque
n,m = map(int, sys.stdin.readline().split())
arr = []
for i in range(n):
    arr.append(list(map(int,sys.stdin.readline().strip())))

# <span id="yx"></span>y,x
ways = [[-1,0],[1,0],[0,-1],[0,1]]

# <span id="now"></span>now
ny = 0
nx = 0

def bfs(y,x):
    q = deque(<a href="/pages/y%2Cx.html" class="wiki-link">y,x</a>)
    arr[y][x] = -1
    while len(q)>0:
        ty,tx = q.popleft()
        step = arr[ty][tx]
        for w in ways:
            dy = ty + w[0]
            dx = tx + w[1] 
            if dy>-1 and dx> -1 and dy<n and dx<m:
                if arr[dy][dx] > 0:
                    q.append([dy,dx])
                    arr[dy][dx] = step-1

bfs(0,0)
print(-arr[n-1][m-1])