๐ ํ ๋งํ
- ์๊ฐ ์ ํ: 1์ด
- ๋ฉ๋ชจ๋ฆฌ ์ ํ: 256MB
๐ ๋ฌธ์
- ์ฒ ์์ ํ ๋งํ ๋์ฅ์์๋ ํ ๋งํ ๋ฅผ ๋ณด๊ดํ๋ ํฐ ์ฐฝ๊ณ ๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ํ ๋งํ ๋ ์๋์ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ๊ฒฉ์ ๋ชจ์ ์์์ ์นธ์ ํ๋์ฉ ๋ฃ์ด์ ์ฐฝ๊ณ ์ ๋ณด๊ดํ๋ค.
- ์ฐฝ๊ณ ์ ๋ณด๊ด๋๋ ํ ๋งํ ๋ค ์ค์๋ ์ ์ต์ ๊ฒ๋ ์์ง๋ง, ์์ง ์ต์ง ์์ ํ ๋งํ ๋ค๋ ์์ ์ ์๋ค.
- ๋ณด๊ด ํ ํ๋ฃจ๊ฐ ์ง๋๋ฉด, ์ต์ ํ ๋งํ ๋ค์ ์ธ์ ํ ๊ณณ์ ์๋ ์ต์ง ์์ ํ ๋งํ ๋ค์ ์ต์ ํ ๋งํ ์ ์ํฅ์ ๋ฐ์ ์ต๊ฒ ๋๋ค.
- ํ๋์ ํ ๋งํ ์ ์ธ์ ํ ๊ณณ์ ์ผ์ชฝ, ์ค๋ฅธ์ชฝ, ์, ๋ค ๋ค ๋ฐฉํฅ์ ์๋ ํ ๋งํ ๋ฅผ ์๋ฏธํ๋ค.
- ๋๊ฐ์ ๋ฐฉํฅ์ ์๋ ํ ๋งํ ๋ค์๊ฒ๋ ์ํฅ์ ์ฃผ์ง ๋ชปํ๋ฉฐ, ํ ๋งํ ๊ฐ ํผ์ ์ ์ ๋ก ์ต๋ ๊ฒฝ์ฐ๋ ์๋ค๊ณ ๊ฐ์ ํ๋ค.
- ์ฒ ์๋ ์ฐฝ๊ณ ์ ๋ณด๊ด๋ ํ ๋งํ ๋ค์ด ๋ฉฐ์น ์ด ์ง๋๋ฉด ๋ค ์ต๊ฒ ๋๋์ง, ๊ทธ ์ต์ ์ผ์๋ฅผ ์๊ณ ์ถ์ด ํ๋ค.
- ํ ๋งํ ๋ฅผ ์ฐฝ๊ณ ์ ๋ณด๊ดํ๋ ๊ฒฉ์๋ชจ์์ ์์๋ค์ ํฌ๊ธฐ์ ์ต์ ํ ๋งํ ๋ค๊ณผ ์ต์ง ์์ ํ ๋งํ ๋ค์ ์ ๋ณด๊ฐ ์ฃผ์ด์ก์ ๋, ๋ฉฐ์น ์ด ์ง๋๋ฉด ๋ชจ๋ ์ต๋์ง, ๊ทธ ์ต์ ์ผ์๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ.
- ๋จ, ์์์ ์ผ๋ถ ์นธ์๋ ํ ๋งํ ๊ฐ ๋ค์ด์์ง ์์ ์๋ ์๋ค.
์ ๋ ฅ
- ์ฒซ ์ค์๋ ์์์ ํฌ๊ธฐ๋ฅผ ๋ํ๋ด๋ ๋ ์ ์ M, N์ด ์ฃผ์ด์ง๋ค.
- M์ ์์์ ๊ฐ๋ก ์นธ์ ์, N์ ์์์ ์ธ๋ก ์นธ์ ์๋ฅผ ๋ํ๋ธ๋ค.
- ๋จ, 2 <= M, N <= 1000 ์ด๋ค. ๋์งธ ์ค๋ถํฐ๋ ํ๋์ ์์์ ์ ์ฅ๋ ํ ๋งํ ๋ค์ ์ ๋ณด๊ฐ ์ฃผ์ด์ง๋ค. ์ฆ, ๋์งธ ์ค๋ถํฐ N๊ฐ์ ์ค์๋ ์์์ ๋ด๊ธด ํ ๋งํ ์ ์ ๋ณด๊ฐ ์ฃผ์ด์ง๋ค. ํ๋์ ์ค์๋ ์์ ๊ฐ๋ก์ค์ ๋ค์ด์๋ ํ ๋งํ ์ ์ํ๊ฐ M๊ฐ์ ์ ์๋ก ์ฃผ์ด์ง๋ค. ์ ์ 1์ ์ต์ ํ ๋งํ , ์ ์ 0์ ์ต์ง ์์ ํ ๋งํ , ์ ์ -1์ ํ ๋งํ ๊ฐ ๋ค์ด์์ง ์์ ์นธ์ ๋ํ๋ธ๋ค.
- ํ ๋งํ ๊ฐ ํ๋ ์ด์ ์๋ ๊ฒฝ์ฐ๋ง ์ฃผ์ด์ง๋ค.
์ถ๋ ฅ
- ์ฌ๋ฌ๋ถ์ ํ ๋งํ ๊ฐ ๋ชจ๋ ์ต์ ๋๊น์ง์ ์ต์ ๋ ์ง๋ฅผ ์ถ๋ ฅํด์ผ ํ๋ค.
- ๋ง์ฝ, ์ ์ฅ๋ ๋๋ถํฐ ๋ชจ๋ ํ ๋งํ ๊ฐ ์ต์ด์๋ ์ํ์ด๋ฉด 0์ ์ถ๋ ฅํด์ผ ํ๊ณ , ํ ๋งํ ๊ฐ ๋ชจ๋ ์ต์ง๋ ๋ชปํ๋ ์ํฉ์ด๋ฉด -1์ ์ถ๋ ฅํด์ผ ํ๋ค.
๐ก ๋ฌธ์ ํ์ด ์์ด๋์ด
- ์๊ณ ๋ฆฌ์ฆ: BFS
- end ์กฐ๊ฑด:
- ํ ๋งํ ๊ฐ ๋ชจ๋ ์ต์ ์ํ: set(Tomato) == {1, -1} ์ธ ๊ฒฝ์ฐ
- ํ ๋งํ ๊ฐ ๋ชจ๋ ์ต์ง ๋ชปํ๋ ์ํฉ: 0์ธ ํ ๋งํ ์ฃผ์์ -1๋ง ์๋ ๊ฒฝ์ฐ
โ 85% ํ๋ ธ์ต๋๋ค ์ฝ๋
- ์ด๋ค ์์ธ์ฒ๋ฆฌ๋ฅผ ๋ชปํ์์ง ๊ณ ๋ฏผ,,
- ๋ง์ง๋ง์ bfs๋๋ฆฐ ํ ๋ชจ๋ ํ ๋งํ ๊ฐ ์ต์๋์ง ์์ต์๋์ง ํ์ธํ๋ ์ฝ๋๋ฅผ ๋นผ๋จน์
from sys import stdin
from collections import deque
input = stdin.readline
M, N = map(int, input().split())
# M ๊ฐ๋ก, N ์ธ๋ก
tomatoes = []
set_tomatoes = set()
day = 0
for _ in range(N):
inputs = list(map(int, input().split()))
set_tomatoes.update(set(inputs))
tomatoes.append(inputs)
# ํ์ฌ ๋ชจ๋ ํ ๋งํ ๊ฐ ์์๋์ง ํ์ธํ๊ธฐ
if len(set_tomatoes) == 1 and set_tomatoes.pop() == 1:
print(0)
exit()
# ์ต์ ํ ๋งํ ๊ฐ ์๋ ๊ฒฝ์ฐ
if 1 not in set_tomatoes:
print(-1)
exit()
queue = deque([])
# ์ต์ ํ ๋งํ ๋ค queue์ ๋ด๊ธฐ
for x in range(N):
for y in range(M):
if tomatoes[x][y] == 1:
queue.append((x, y))
# bfs
def bfs(graph, queue):
# ์ ํ ์ข ์ฐ
directionX = [-1, 1, 0, 0]
directionY = [0, 0, -1, 1]
next = deque([])
while queue:
x, y = queue.popleft()
for i in range(len(directionX)):
nx, ny = x + directionX[i], y + directionY[i]
if 0 <= nx < N and 0 <= ny < M and graph[nx][ny] == 0:
graph[nx][ny] = 1
next.append((nx, ny))
return graph, next
while queue:
tomatoes, queue = bfs(tomatoes, queue)
day += 1
# print(day, queue)
#
# for tomato in tomatoes:
# print(tomato)
print(day - 1)
โ ์ ๋ต ์ฝ๋
from sys import stdin
from collections import deque
input = stdin.readline
M, N = map(int, input().split())
# M ๊ฐ๋ก, N ์ธ๋ก
tomatoes = []
set_tomatoes = set()
day = 0
for _ in range(N):
inputs = list(map(int, input().split()))
set_tomatoes.update(set(inputs))
tomatoes.append(inputs)
# ํ์ฌ ๋ชจ๋ ํ ๋งํ ๊ฐ ์์๋์ง ํ์ธํ๊ธฐ
if len(set_tomatoes) == 1 and set_tomatoes.pop() == 1:
print(0)
exit()
# ์ต์ ํ ๋งํ ๊ฐ ์๋ ๊ฒฝ์ฐ
if 1 not in set_tomatoes:
print(-1)
exit()
queue = deque([])
# ์ต์ ํ ๋งํ ๋ค queue์ ๋ด๊ธฐ
for x in range(N):
for y in range(M):
if tomatoes[x][y] == 1:
queue.append((x, y))
# bfs
def bfs(graph, queue):
# ์ ํ ์ข ์ฐ
directionX = [-1, 1, 0, 0]
directionY = [0, 0, -1, 1]
next = deque([])
while queue:
x, y = queue.popleft()
for i in range(len(directionX)):
nx, ny = x + directionX[i], y + directionY[i]
if 0 <= nx < N and 0 <= ny < M and graph[nx][ny] == 0:
graph[nx][ny] = 1
next.append((nx, ny))
return graph, next
while queue:
tomatoes, queue = bfs(tomatoes, queue)
day += 1
for tomato in tomatoes:
# ์ต์ง ์์ ํ ๋งํ ๊ฐ ์๋์ง ํ์ธ
if 0 in tomato:
print(-1)
exit()
print(day - 1)
728x90
'Algorithm&CodingTest > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Beakjoon][14502] ๊ณจ๋4 ์ฐ๊ตฌ์ - Python (0) | 2024.10.08 |
---|---|
[Baekjoon][2206] ๊ณจ๋3 ๋ฒฝ ๋ถ์๊ณ ์ด๋ํ๊ธฐ - Python (0) | 2024.10.08 |
[Beakjoon][19637] ์ค๋ฒ3 - IF๋ฌธ ์ข ๋์ ์จ์ค Python (0) | 2024.05.05 |
[Baekjoon] [20310] ์ค๋ฒ3 - ํ๋ ธ์ค Python (0) | 2024.05.05 |
[Baekjoon] [21921] ์ค๋ฒ3 - ๋ธ๋ก๊ทธ Python (1) | 2024.01.31 |