[Silver I] 14940 - ์ฌ์ด ์ต๋จ๊ฑฐ๋ฆฌ
๋ฌธ์
์ง๋๊ฐ ์ฃผ์ด์ง๋ฉด ๋ชจ๋ ์ง์ ์ ๋ํด์ ๋ชฉํ์ง์ ๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ์ฌ๋ผ.
๋ฌธ์ ๋ฅผ ์ฝ๊ฒ ๋ง๋ค๊ธฐ ์ํด ์ค์ง ๊ฐ๋ก์ ์ธ๋ก๋ก๋ง ์์ง์ผ ์ ์๋ค๊ณ ํ์.
์ ๋ ฅ
์ง๋์ ํฌ๊ธฐ n๊ณผ m์ด ์ฃผ์ด์ง๋ค. n์ ์ธ๋ก์ ํฌ๊ธฐ, m์ ๊ฐ๋ก์ ํฌ๊ธฐ๋ค.(2 ≤ n ≤ 1000, 2 ≤ m ≤ 1000)
๋ค์ n๊ฐ์ ์ค์ m๊ฐ์ ์ซ์๊ฐ ์ฃผ์ด์ง๋ค. 0์ ๊ฐ ์ ์๋ ๋ ์ด๊ณ 1์ ๊ฐ ์ ์๋ ๋ , 2๋ ๋ชฉํ์ง์ ์ด๋ค. ์ ๋ ฅ์์ 2๋ ๋จ ํ๊ฐ์ด๋ค.
์ถ๋ ฅ
๊ฐ ์ง์ ์์ ๋ชฉํ์ง์ ๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ์ถ๋ ฅํ๋ค. ์๋ ๊ฐ ์ ์๋ ๋ ์ธ ์์น๋ 0์ ์ถ๋ ฅํ๊ณ , ์๋ ๊ฐ ์ ์๋ ๋ ์ธ ๋ถ๋ถ ์ค์์ ๋๋ฌํ ์ ์๋ ์์น๋ -1์ ์ถ๋ ฅํ๋ค.
์์ ์ ๋ ฅ 1 ๋ณต์ฌ
15 15
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1 0 1 0 0 0
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
์์ ์ถ๋ ฅ 1 ๋ณต์ฌ
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
11 12 13 14 15 16 17 18 19 20 0 0 0 0 25
12 13 14 15 16 17 18 19 20 21 0 29 28 27 26
13 14 15 16 17 18 19 20 21 22 0 30 0 0 0
14 15 16 17 18 19 20 21 22 23 0 31 32 33 34
# ํ์ด ๋ฐฉ๋ฒ
- ์์์์น์ธ ‘2’๋ฅผ ์ฐพ๋๋ค
- ์์์์น๋ก๋ถํฐ ๋ค๋ฅธ ์ง์ ๊ณผ์ ๊ฑฐ๋ฆฌ๋ฅผ dist๋ฐฐ์ด์ ๋ด๋๋ค
# ์ฝ๋
# 2025-05-18 10:30 - 11:05
from collections import deque
import sys
sys.stdin = open("input.txt","r")
n, m = map(int,sys.stdin.readline().split())
grid = [list(map(int,sys.stdin.readline().split())) for _ in range(n)]
dist = [[-1] * m for _ in range(n)]
dx = [-1,1,0,0]
dy = [0,0,-1,1]
def bfs(x,y):
queue = deque()
queue.append((x,y))
dist[x][y] = 0 # ์์์ ์ ๊ฑฐ๋ฆฌ 0
while queue:
x,y = queue.popleft()
for d in range(4):
nx, ny = x + dx[d], y + dy[d]
if 0 <= nx < n and 0 <= ny < m:
if grid[nx][ny] in (1,2) and dist[nx][ny] == -1:
dist[nx][ny] = dist[x][y] + 1
queue.append((nx,ny))
return 0
# ์์ ์ง์ ์ฐพ๊ธฐ (๊ฐ์ด 2์ธ ๊ณณ)
for i in range(n):
for j in range(m):
if grid[i][j] == 2:
bfs(i, j)
for i in range(n):
for j in range(m):
if grid[i][j] == 0:
print(0, end = ' ')
else:
print(dist[i][j], end = ' ')
print()
# ์ฐธ๊ณ
์ฒ์์ ์ ์ด๋ฐ์์ผ๋ก ํ์์ง..? 2 ์ง์ ์ ๋จผ์ ์ฐพ์์ด์ผ ํ๋ค
# 2025-05-18 10:30 -11:03
from collections import deque
import sys
# sys.stdin = open("input.txt","r")
n, m = map(int,sys.stdin.readline().split())
grid = [[0]*m for _ in range(n)]
for i in range(n):
grid[i] = list(map(int,sys.stdin.readline().split()))
dx = [-1,1,0,0]
dy = [0,0,-1,1]
def bfs(x,y):
# ๊ฐ ์ ์๋ ๋
์ธ ๊ฒฝ์ฐ
if grid[x][y] == 0:
return 0
visited = [[False]*m for _ in range(n)]
queue = deque()
queue.append((x,y,0))
visited[x][y] = True
while queue:
x,y,cnt = queue.popleft()
if grid[x][y] == 2:
return cnt
for d in range(4):
nx, ny = x + dx[d], y + dy[d]
if 0 <= nx < n and 0 <= ny < m:
if grid[nx][ny] in (1,2) and not visited[nx][ny]:
visited[nx][ny] = True
queue.append((nx,ny,cnt+1))
return 0
for i in range(n):
for j in range(m):
cnt = bfs(i,j)
print(cnt, end = ' ')
print()