practivceAlgorithm/백준

[백준][Python] 14930 쉬운최단거리

findTheValue 2021. 7. 30. 17:45

2를 탐색해준후 BFS로 거리에따른 숫자 기록을 진행해줬다.

처음엔 2방으로 했었는데 하고보니 막혀있는 구간에서 사방 탐색이 필요해서 사방으로 바꿔줬다

import sys
input = sys.stdin.readline
from collections import deque

def BFS(i,j):
    global dp_dists
    queue = deque()
    queue.append([i,j])
    dp_dists[i][j] = 0
    while queue:
        x,y = queue.popleft()
        for i in range(4):
            nx = x+dx[i]
            ny = y+dy[i]
            if 0<=nx<n and 0<=ny<m and dp_dists[nx][ny] == -1 :
                queue.append([nx,ny])
                dp_dists[nx][ny] = dp_dists[x][y]+1

def find_start():
    for i in range(n):
        for j in range(m):
            if not maps[i][j]:
                dp_dists[i][j]=0
    for i in range(n):
        for j in range(m):
            if maps[i][j]==2:
                BFS(i,j)
                return

n,m = map(int,input().split())
maps = [list(map(int,input().split())) for _ in range(n)]
dp_dists = [[-1 for _ in range(m)] for _ in range(n)]
dx = [0,1,-1,0]
dy = [1,0,0,-1]

find_start()

for i in range(n):
    print(*dp_dists[i])