백준 문제 풀이
백준 1261 파이썬 python : 알고스팟 @@황소처럼 우직하게@@ 화이팅!
hjp845
2020. 3. 3. 16:46
반응형
고진감래다
import sys
from collections import deque
input = sys.stdin.readline
w, h = map(int, input().split())
miro = [[] for i in range(h)]
for i in range(h):
ss = input().strip()
for s in ss:
miro[i].append(int(s))
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
ans = 999999999
def bfs(y, x):
q = [[y, x]]
nxtq = []
miro[y][x] = 2
while q:
now = q.pop(0)
for i in range(4):
ny = now[0] + dy[i]
nx = now[1] + dx[i]
if 0 <= ny < h and 0 <= nx < w:
if miro[ny][nx] == 0:
miro[ny][nx] = miro[now[0]][now[1]]
q.append([ny, nx])
elif miro[ny][nx] == 1:
miro[ny][nx] = miro[now[0]][now[1]] + 1
nxtq.append([ny, nx])
if not q:
q = nxtq
nxtq = []
bfs(0, 0)
print(miro[-1][-1] - 2)
반응형