# 1은 갈 수 있는 땅, 0은 갈 수 없는 땅, 2는 목표 지점
# n은 세로 크기, m은 가로 크기
from collections import deque
import sys
input = sys.stdin.readline
n,m = map(int, input().split())#n은 y m은 x
mapper = []
for i in range(n):
mapper.append(list(map(int, input().split())))
dx=(0,0,-1,1)
dy=(1,-1,0,0)
a=deque()
for y in range(n):
for x in range(m):
if mapper[y][x]==2:
a.append([y,x])
def bfs():
while len(a):
y,x = a.popleft()
for i in range(4):
new_x = x+dx[i]
new_y = y+dy[i]
if new_x>=m or new_y >=n or new_x<=-1 or new_y <= -1:
continue
else:
if mapper[new_y][new_x]==1:
mapper[new_y][new_x] = mapper[y][x]+1
a.append([new_y,new_x])
bfs()
for i in range(len(mapper)):
for j in range(len(mapper[i])):
if mapper[i][j]==0:
continue
else:
mapper[i][j]= mapper[i][j]-2
for c in mapper:
print(*c)
댓글남기기