From cadfcc9c54a9aa11029ca524fa07ac5e6dba2b51 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Fri, 8 Aug 2025 22:24:29 +0900 Subject: [PATCH] 20250808 baekjoon --- code_study/Baekjoon/python/14940.py | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 code_study/Baekjoon/python/14940.py diff --git a/code_study/Baekjoon/python/14940.py b/code_study/Baekjoon/python/14940.py new file mode 100644 index 0000000..1c8ee68 --- /dev/null +++ b/code_study/Baekjoon/python/14940.py @@ -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) \ No newline at end of file