b_쉬운 최단거리_14940

source www.acmicpc.net/problem/14940
topics
types 문제풀이
정답여부 성공

문제

import sys
from collections import deque
N,M = map(int,input().strip().split())
arr = []
destination = []
for i in range(N):
    line = list(map(int,input().strip().split()))
    try:
        x=line.index(2)
        destination = [i,x]
        line[x] = 0
    except:
        pass
    arr.append(line)

def checkCanMove(y,x):
    if y<0 or x<0:
        return False
    if y>= len(arr) or x>=len(arr[0]):
        return False
    if arr[y][x]<1:
        return False
    return True

que = deque([destination])
# print(que)
move = [[0,1],[0,-1],[1,0],[-1,0]]
while que:
    y,x = que.popleft()
    val = arr[y][x]
    # print(y,x,val)
    for m in move:
        ny = y + m[0] 
        nx = x + m[1] 
        if checkCanMove(ny,nx):
            arr[ny][nx] = val-1
            que.append([ny,nx])

for line in arr:
    output = ""
    for l in line:
        if l < 0:
            l = -l
        elif l > 0:
            l = -1
        output += str(l)+' '
    print(output)