20250808 baekjoon

This commit is contained in:
songyc macbook 2025-08-08 22:24:29 +09:00
parent 5c9e19e248
commit cadfcc9c54

View File

@ -0,0 +1,36 @@
import sys
from collections import deque
input = sys.stdin.readline
N, M = map(int, input().split())
Map = []
for i in range(N) :
Map.append(list(map(int, input().split())))
for j in range(M) :
if Map[i][j] == 2 :
start = (j,i)
q = deque()
q.append(start)
visited = [[-1]*M for _ in range(N)]
startX, startY = start
visited[startY][startX] = 0
dxList = [1,-1,0,0]
dyList = [0,0,1,-1]
while len(q) != 0 :
currentX, currentY = q.popleft()
for dx, dy in zip(dxList,dyList):
nextX, nextY = currentX + dx, currentY + dy
if 0 <= nextX < M and 0<= nextY < N and visited[nextY][nextX] == -1 and Map[nextY][nextX] != 0 :
q.append((nextX,nextY))
visited[nextY][nextX] = visited[currentY][currentX] + 1
for i in range(N):
for j in range(M):
if Map[i][j] == 0 :
visited[i][j] = 0
for row in visited :
print(*row)