# 7569 토마토 M,N,H = map(int,input().split()) dx = [-1,1,0,0,0,0] dy = [0,0,-1,1,0,0] dz = [0,0,0,0,-1,1] tomatoarray =[ [list(map(int,input().split())) for _ in range(N)] for _ in range(H)] # tomato dz dx dy # H,N,M total = N*M*H tomatocnt = 0 tomatos = [] result = -1 day = 0 for x in range(N): for y in range(M): for z in range(H): if tomatoarray[z][x][y] == 1: tomatos.append((x,y,z)) tomatocnt += 1 elif tomatoarray[z][x][y] == -1: total -= 1 if total == tomatocnt: result = 0 else: while tomatos: new_tomato = [] for x,y,z in tomatos: for i in range(6): nx = x + dx[i] ny = y + dy[i] nz = z + dz[i] if 0<=nx<N and 0<=ny<M and 0<=nz<H: if not tomatoarray[nz][nx][ny]: tomatoarray[nz][nx][ny] = 1 tomatocnt += 1 new_tomato.append((nx,ny,nz)) day += 1 if tomatocnt == total: result = day break if len(new_tomato): tomatos = [row[:] for row in new_tomato] else: result = -1 break print(result)
이 문제는 7576번 문제의 3D 버전이다 여기서 조심해야할 것은 3차원이고, x,y,z의 순서를 조심해주면 된다.
보통 문제를 풀때 나같은 경우 x축을 행으로 하고 y축을 열로 한다. 자신만의 기준을 가지고, 헷갈리지 않게 조심하면 될 것 같다.
기본 원리는 7576번하고 같다.
'알고리즘 > 백준' 카테고리의 다른 글
[BOJ] 9498 시험성적 (0) | 2021.01.09 |
---|---|
[BOJ] 7576 토마토 (0) | 2021.01.09 |
[BOJ] 2751 수 정렬하기 2 (0) | 2021.01.09 |
[BOJ] 2606 바이러스 (0) | 2021.01.09 |
[BOJ] 2178 미로탐색 (0) | 2021.01.09 |