λΆλ Έμ ν λ§ν ...π
μ κ·Όλ°©λ²μ΄ νλ €μ μκ³ λ¦¬μ¦μ λͺλ²μ© μμ νλ€
πΌ λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/7569
- ν°μ΄: 골λ V
- λΆλ₯: κ·Έλν μ΄λ‘ , κ·Έλν νμ, λλΉ μ°μ νμ
β TRIAL 1.
μ½λ
import sys
input = sys.stdin.readline
from collections import deque
M, N, H = map(int, input().split())
#fill tomatos (-1: no tomato, 0: raw tomato, 1: grown tomato)
tmt = [[[0 for _ in range(M)] for _ in range(N)] for _ in range(H)]
for h in range(H):
for n in range(N):
tmt[h][n] = list(map(int, input().split()))
#BFS
adjs = [(1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)]
def BFS(v):
visited = [[[False for _ in range(M)] for _ in range(N)] for _ in range(H)]
queue = deque([(v, 0)])
visited[v[0]][v[1]][v[2]] = True
while queue:
v, cnt = queue.popleft()
for d in adjs:
new_v = [v[i]+d[i] for i in range(3)]
if 0<=new_v[0]<H and 0<=new_v[1]<N and 0<=new_v[2]<M:
if tmt[new_v[0]][new_v[1]][new_v[2]] == 1:
return cnt+1
elif not visited[new_v[0]][new_v[1]][new_v[2]] and tmt[new_v[0]][new_v[1]][new_v[2]] == 0:
visited[new_v[0]][new_v[1]][new_v[2]] = True
queue.append([new_v, cnt+1])
return -1
def solution():
maxcount = 0
for h in range(H):
for n in range(N):
for m in range(M):
if tmt[h][n][m] == 0:
cnt = BFS([h, n, m])
if cnt == -1:
return -1
if cnt > maxcount:
maxcount = cnt
return maxcount
print(solution())
μ½λ μ€λͺ
- μ΅μ ν λ§ν μμ ν μ€ν μ© μ΅νκ°λκ² μλλΌ,
- λͺ¨λ μμ΅μ ν λ§ν μ λν΄ μ΅μ ν λ§ν κΉμ§μ μ΅λ¨κ±°λ¦¬λ₯Ό ꡬνλ λ°©μμΌλ‘ ν
νλ¦° μμΈ
μκ°μ΄κ³Ό. κ·Έλ₯ λ¬Έμ μμ μ£Όμ΄μ§ λλ‘ μ΅μ ν λ§ν μμ ν μ€ν μ© λμκ°λ κ² λ λΉ λ₯΄λ€.
β TRIAL 2.
μ½λ
import sys
input = sys.stdin.readline
from collections import deque
M, N, H = map(int, input().split())
#fill tomatos (-1: no tomato, 0: raw tomato, 1: grown tomato)
tmt = [[[0 for _ in range(M)] for _ in range(N)] for _ in range(H)]
for h in range(H):
for n in range(N):
tmt[h][n] = list(map(int, input().split()))
#list of grown tomatos
growns = []
num_target = 0
for h in range(H):
for n in range(N):
for m in range(M):
if tmt[h][n][m] == 1:
growns.append((h, n, m))
elif tmt[h][n][m] == 0:
num_target += 1
dhs = [1, -1, 0, 0, 0, 0]
dns = [0, 0, 1, -1, 0, 0]
dms = [0, 0, 0, 0, 1, -1]
cnt = 0
while num_target > 0:
new_target = num_target
growns_tmp = []
for vh, vn, vm in growns:
for dh, dn, dm in zip(dhs, dns, dms):
newh, newn, newm = vh+dh, vn+dn, vm+dm
if 0<=newh<H and 0<=newn<N and 0<=newm<M:
if tmt[newh][newn][newm] == 0:
tmt[newh][newn][newm] = 1
new_target -= 1
growns_tmp.append((newh, newn, newm))
if new_target == num_target:
cnt = -1
break
growns.extend(growns_tmp)
num_target = new_target
cnt += 1
print(cnt)
μμ ν λΆλΆ
- μ΅μ ν λ§ν μ λͺ©λ‘μ μ²μμ μ μ₯νκ³ ,
- 맀 stepλ§λ€ μΈμ ν μ΅μ§ μμ ν λ§ν λ€μ μ΅νλ€.
- μ²μμ μ΅μ§ μμ ν λ§ν μ κ°―μλ₯Ό countνκ³ ,
- 맀 stepμ΄ λλ λ μ΅μ§ μμ ν λ§ν μ κ°―μκ° μ€μ§ μμμΌλ©΄ μ§νμ λ©μΆκ³ -1μ λ°ν, (λμ΄μ μ§νν μ μμ)
- μ΅μ§ μμ ν λ§ν κ° μμΌλ©΄ μ΄ step μλ₯Ό returnνλ€.
νλ¦° μμΈ
μκ°μ΄κ³Ό. 맀 stepλ§λ€, νλ² κ²μ¬ν μ΅μ ν λ§ν λ λ€μλ²μ κ²μ¬νμ§ μμλ λ¨ (μ΄λ―Έ μΈμ ν λ§ν λ λ€ μ΅μμΌλκΉ)
π‘ Solution
μ½λ
import sys
input = sys.stdin.readline
from collections import deque
M, N, H = map(int, input().split())
#fill tomatos (-1: no tomato, 0: raw tomato, 1: grown tomato)
tmt = [[[0 for _ in range(M)] for _ in range(N)] for _ in range(H)]
for h in range(H):
for n in range(N):
tmt[h][n] = list(map(int, input().split()))
growns = deque([])
num_target = 0
for h in range(H):
for n in range(N):
for m in range(M):
if tmt[h][n][m] == 1:
growns.append((h, n, m))
elif tmt[h][n][m] == 0:
num_target += 1
dhs = [1, -1, 0, 0, 0, 0]
dns = [0, 0, 1, -1, 0, 0]
dms = [0, 0, 0, 0, 1, -1]
cnt = 0
while num_target > 0:
new_target = num_target
new_growns = deque([])
while growns:
vh, vn, vm = growns.popleft()
for dh, dn, dm in zip(dhs, dns, dms):
newh, newn, newm = vh+dh, vn+dn, vm+dm
if 0<=newh<H and 0<=newn<N and 0<=newm<M:
if tmt[newh][newn][newm] == 0:
tmt[newh][newn][newm] = 1
new_target -= 1
new_growns.append((newh, newn, newm))
if new_target == num_target:
cnt = -1
break
num_target = new_target
growns = new_growns
cnt += 1
print(cnt)
μ½λ μ€λͺ
- μ΅μ ν λ§ν μ λͺ©λ‘μ μ²μμ μ μ₯νκ³ (
growns
), - 맀 stepλ§λ€ μ΅μ ν λ§ν μ μΈμ ν μ΅μ§ μμ ν λ§ν λ€μ μ΅νλ€. μ΄ λ μλ‘κ² μ΅μ ν λ§ν λ€μ λͺ©λ‘μ μ μ₯νλ€ (
new_gronws
). - μ²μμ μ΅μ§ μμ ν λ§ν μ κ°―μλ₯Ό countνκ³ (
num_target
), - 맀 stepμ΄ λλ λ μ΅μ§ μμ ν λ§ν μ κ°―μκ° μ€μ§ μμμΌλ©΄ (
if new_target == num_target
) μ§νμ λ©μΆκ³ -1μ λ°ν, (λμ΄μ μ§νν μ μμ) - μ΅μ§ μμ ν λ§ν κ° μμΌλ©΄ μ΄ step μλ₯Ό returnνλ€.
- λ€μ stepμμλ μλ‘κ² μ΅μ ν λ§ν λ€μ λͺ©λ‘μ μ΄μ©ν΄ λμΌνκ² μ§ννλ€.
'π CS > μ½λ©ν μ€νΈ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€ 2579λ²] κ³λ¨ μ€λ₯΄κΈ° (python/νμ΄μ¬) (0) | 2022.09.15 |
---|---|
[λ°±μ€ 14500λ²] ν νΈλ‘λ―Έλ Έ (python/νμ΄μ¬) (2) | 2022.09.14 |
[λ°±μ€ 16926λ²] λ°°μ΄ λ리기 1 (python/νμ΄μ¬) (0) | 2022.07.05 |
[λ°±μ€ 2504λ²] κ΄νΈμ κ° (python/νμ΄μ¬) (0) | 2022.07.01 |
[λ°±μ€ 1697λ²] μ¨λ°κΌμ§ (python/νμ΄μ¬) (0) | 2022.06.30 |