๋ฐฑ์ค 7576๋ฒ ํ ๋งํ (BFS ๋ฌธ์ ) _ ํ์ด์ฌ
from collections import deque
# ์
๋ ฅ ๋ฐ๊ธฐ
M, N = map(int, input().split())
tomato = [list(map(int, input().split())) for _ in range(N)]
# 4๋ฐฉํฅ(์, ํ, ์ข, ์ฐ) ์ ์
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
# ์ต์ ํ ๋งํ ์ ์์น ํ์ ๋ฃ๊ธฐ
queue = deque()
# ์ด๊ธฐ ์ต์ ํ ๋งํ ์์น ์ฐพ๊ธฐ
for i in range(N):
for j in range(M):
if tomato[i][j] == 1: # ์ต์ ํ ๋งํ ์ผ ๊ฒฝ์ฐ
queue.append((i, j))
# BFS ์์
def bfs():
# ํ๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณต
while queue:
x, y = queue.popleft()
# 4๋ฐฉํฅ ํ์
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
# ๋ฒ์ ์ฒดํฌ
if 0 <= nx < N and 0 <= ny < M:
if tomato[nx][ny] == 0: # ์์ง ์ต์ง ์์ ํ ๋งํ
tomato[nx][ny] = tomato[x][y] + 1 # 1์ด ์ฆ๊ฐ์ํค๋ฉด์ ์ตํ๊ธฐ
queue.append((nx, ny)) # ์ต์ ํ ๋งํ ๋ฅผ ํ์ ์ถ๊ฐ
# BFS ์คํ
bfs()
# ๊ฒฐ๊ณผ ํ์ธ: ํ ๋งํ ๊ฐ ๋ชจ๋ ์ต์๋์ง ํ์ธ
max_days = 0
for i in range(N):
for j in range(M):
if tomato[i][j] == 0: # ์์ง ์ต์ง ์์ ํ ๋งํ ๊ฐ ์๋ค๋ฉด
print(-1)
exit(0)
max_days = max(max_days, tomato[i][j]) # ๊ฐ์ฅ ํฐ ๊ฐ(์ต๋ ์ผ์)์ ๊ตฌํจ
# ๋ง์ง๋ง์ -1์ ๋นผ์ค์ผ 0์ด๋ถํฐ ์์ํ ์๊ฐ์ ๋ง์ถ ์ ์์
print(max_days - 1)
1๏ธโฃ ๋ฌธ์ ์ดํดํ๊ธฐ
๐น ๋ฌธ์ ์์ฝ
- ์ฐฝ๊ณ ์ ์ต์ ํ ๋งํ (1), ์ ์ต์ ํ ๋งํ (0), ๋น ์นธ(-1) ๊ฐ ์๋ค.
- ์ต์ ํ ๋งํ (1)๋ ํ๋ฃจ๊ฐ ์ง๋๋ฉด ์ํ์ข์ฐ์ ์ ์ต์ ํ ๋งํ (0)๋ฅผ ์ต๊ฒ ๋ง๋ ๋ค.
- ๋ชจ๋ ํ ๋งํ ๊ฐ ์ต๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์ต์ ๋ ์ง๋ฅผ ๊ตฌํ๋ ๋ฌธ์ !
๐น ์ ๋ ฅ ์์
6 4 # ๊ฐ๋ก(์ด) M=6, ์ธ๋ก(ํ) N=4
0 0 -1 0 0 0
0 0 1 0 -1 0
0 0 -1 0 0 0
0 0 0 0 -1 1
๐น ์ถ๋ ฅ ์์
4 # (๋ชจ๋ ํ ๋งํ ๊ฐ ์ต๋ ๋ฐ ๊ฑธ๋ฆฐ ์ต์ ๋ ์ง)
2๏ธโฃ BFS๋ฅผ ์ฌ์ฉํ๋ ์ด์
- BFS(๋๋น ์ฐ์ ํ์) ๋ ์ต๋จ ๊ฑฐ๋ฆฌ ํ์์ ์ ํฉํ ์๊ณ ๋ฆฌ์ฆ์ด์ผ.
- ํ ๋งํ ๋ ๋์์ ์ฌ๋ฌ ๋ฐฉํฅ(์ํ์ข์ฐ)์ผ๋ก ํผ์ ธ๋๊ฐ์ผ ํจ → BFS ์ฌ์ฉ!
(DFS๋ ํ์ชฝ์ผ๋ก๋ง ๊น๊ฒ ํ์ํด์ ์ ํฉํ์ง ์์)
3๏ธโฃ ๋ฌธ์ ํ์ด ๊ณผ์
โ 1๋จ๊ณ: ์ ๋ ฅ ๋ฐ๊ธฐ
from collections import deque
import sys
M, N = map(int, sys.stdin.readline().split()) # ๊ฐ๋ก(M), ์ธ๋ก(N)
tomato = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
๐ ์ด์ฐจ์ ๋ฐฐ์ด(tomato)๋ก ์ฐฝ๊ณ ์ ๋ณด๋ฅผ ์ ์ฅ
โ 2๋จ๊ณ: BFS๋ฅผ ์ํ ์ค๋น (ํ ์ด๊ธฐํ)
queue = deque()
# ์ต์ ํ ๋งํ (1)์ ์์น๋ฅผ ๋ชจ๋ ํ์ ์ถ๊ฐ
for i in range(N):
for j in range(M):
if tomato[i][j] == 1:
queue.append((i, j))
๐ ์ต์ ํ ๋งํ (1)๋ค์ ์์น๋ฅผ ๋ฏธ๋ฆฌ ํ์ ๋ฃ์ → BFS์์ ๋์์ ํผ์ง๊ฒ ํ๊ธฐ ์ํด!
โ 3๋จ๊ณ: 4๋ฐฉํฅ ์ด๋ ์ ์
dx = [-1, 1, 0, 0] # ์, ํ, ์ข, ์ฐ (ํ ๋ฐฉํฅ)
dy = [0, 0, -1, 1] # ์, ํ, ์ข, ์ฐ (์ด ๋ฐฉํฅ)
๐ ํ์ฌ ์์น์์ ์ํ์ข์ฐ๋ก ์ด๋ํ ๋ ์ขํ๋ฅผ ์ฝ๊ฒ ๊ตฌํ๊ธฐ ์ํด ์ฌ์ฉ!
โ 4๋จ๊ณ: BFS ํ์ ์์
def bfs():
while queue:
x, y = queue.popleft() # ํ์์ ํ์ฌ ์์น ๊บผ๋
for i in range(4): # ๋ค ๋ฐฉํฅ ํ์
nx = x + dx[i]
ny = y + dy[i]
# ๋ฒ์ ์์ ์๊ณ , ์ต์ง ์์ ํ ๋งํ (0)์ผ ๊ฒฝ์ฐ
if 0 <= nx < N and 0 <= ny < M and tomato[nx][ny] == 0:
tomato[nx][ny] = tomato[x][y] + 1 # ํ๋ฃจ ์ถ๊ฐ
queue.append((nx, ny)) # ์๋ก์ด ์ต์ ํ ๋งํ ์ถ๊ฐ
๐ ํ์์ ๊บผ๋ธ ์ขํ (x, y)์์ ์ํ์ข์ฐ๋ก ํผ์ ธ ๋๊ฐ
๐ ์ต์ง ์์ ํ ๋งํ (0)๋ฅผ ๋ฐ๊ฒฌํ๋ฉด ํ๋ฃจ ์ถ๊ฐ (tomato[x][y] + 1)
โ 5๋จ๊ณ: BFS ์คํ ํ ์ต๋ ๋ ์ง ์ฐพ๊ธฐ
bfs() # BFS ์คํ
max_day = 0 # ๊ฑธ๋ฆฐ ์ต๋ ์ผ์
for row in tomato:
for t in row:
if t == 0: # ์ต์ง ์์ ํ ๋งํ ๊ฐ ๋จ์์์ผ๋ฉด
print(-1)
exit()
max_day = max(max_day, t)
print(max_day - 1) # ์ฒ์ ์์์ 1๋ก ํ์ผ๋ฏ๋ก 1 ๋นผ์ค
๐ ๋ชจ๋ ํ ๋งํ ๊ฐ ์ต์๋์ง ํ์ธํ๊ณ , ์ต๋ ๋ ์ง ์ถ๋ ฅ!
๐ ํ๋๋ผ๋ 0์ด ๋จ์ ์์ผ๋ฉด -1 ์ถ๋ ฅ (์ต์ ์ ์๋ ํ ๋งํ ์กด์ฌ)
๐ ์ฒ์ ์์์ 1๋ก ์ค์ ํ์ผ๋ฏ๋ก, ๊ฒฐ๊ณผ์์ 1์ ๋นผ์ค!
4๏ธโฃ ์ต์ข ์ฝ๋
from collections import deque
import sys
# ์
๋ ฅ ๋ฐ๊ธฐ
M, N = map(int, sys.stdin.readline().split())
tomato = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
# ํ ์ด๊ธฐํ (์ต์ ํ ๋งํ ์์น ์ ์ฅ)
queue = deque()
for i in range(N):
for j in range(M):
if tomato[i][j] == 1:
queue.append((i, j))
# ๋ค ๋ฐฉํฅ ์ด๋ ์ ์ (์, ํ, ์ข, ์ฐ)
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
# BFS ํ์ ํจ์
def bfs():
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < N and 0 <= ny < M and tomato[nx][ny] == 0:
tomato[nx][ny] = tomato[x][y] + 1
queue.append((nx, ny))
bfs() # BFS ์คํ
# ๊ฒฐ๊ณผ ๊ณ์ฐ
max_day = 0
for row in tomato:
for t in row:
if t == 0:
print(-1) # ์ต์ง ์์ ํ ๋งํ ๊ฐ ์์ผ๋ฉด -1 ์ถ๋ ฅ
exit()
max_day = max(max_day, t)
print(max_day - 1) # ์ฒ์ ์์์ 1๋ก ํ์ผ๋ฏ๋ก 1 ๋นผ์ค
5๏ธโฃ ์ต์ข ์ ๋ฆฌ
โ BFS๋ฅผ ์ฌ์ฉํ๋ ์ด์
- ํ ๋งํ ๊ฐ ๋์์ ์ํ์ข์ฐ๋ก ํผ์ ธ์ผ ํด์ BFS๊ฐ ์ ํฉ!
โ ํ์ด ๊ณผ์
1๏ธโฃ ์
๋ ฅ ๋ฐ๊ธฐ → 2์ฐจ์ ๋ฆฌ์คํธ(tomato) ์์ฑ
2๏ธโฃ ํ ์ด๊ธฐํ → ์ต์ ํ ๋งํ (1) ์์น๋ฅผ ํ์ ๋ฃ๊ธฐ
3๏ธโฃ BFS ํ์ → ์ต์ ํ ๋งํ ๊ฐ ์ํ์ข์ฐ๋ก ํผ์ง
4๏ธโฃ ์ต๋ ๋ ์ง ์ฐพ๊ธฐ → ๋ชจ๋ ํ ๋งํ ๊ฐ ์ต์๋์ง ํ์ธ ํ ์ถ๋ ฅ