20250808 baekjoon
This commit is contained in:
parent
5c9e19e248
commit
cadfcc9c54
36
code_study/Baekjoon/python/14940.py
Normal file
36
code_study/Baekjoon/python/14940.py
Normal 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)
|
||||
Loading…
x
Reference in New Issue
Block a user