5 3 1
0 -1 0 0 0
-1 -1 0 1 1
0 0 0 1 1
-1
BFS함수
1. 만들어둔 토마토클래스를 타입으로 갖는 queue를 생성
2. 반복문 돌면서 익은 토마토들을 queue삽입
3. 이후 queue가 비어있을 때까지 queue의 크기만큼 반복하면서 인접구역을 확인한다.
3.1. 이때 해당 위치가 0이면 익지 않은 것이므로 1로 만들고 queue에 삽입한다.
3.2. 다음 반복할 때마다 day++
3.3. 이후 반복문 종료 후 day 리턴
import java.io.*;
import java.util.*;
class Tomato {
int x,y,z;
public Tomato(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
public class Main {
static int m,n,h;
static int[][][] box;
static int[] dx = {0,0,1,-1,0,0}; //상하우좌위아래
static int[] dy = {1,-1,0,0,0,0};
static int[] dz = {0,0,0,0,1,-1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
m = Integer.parseInt(st.nextToken());
n = Integer.parseInt(st.nextToken());
h = Integer.parseInt(st.nextToken());
box = new int[h][n][m];
for(int i=0; i<h;i++) {
for(int j=0; j<n; j++) {
st = new StringTokenizer(br.readLine());
for(int k=0; k<m; k++)
box[i][j][k] = Integer.parseInt(st.nextToken());
}
}
int day = BFS();
if(checkTomato())
System.out.println(day);
else System.out.println(-1);
}
public static int BFS() {
Queue<Tomato> queue = new ArrayDeque<>();
int day = -1;
for(int i=0; i<h;i++) {
for(int j=0; j<n; j++) {
for(int k=0; k<m; k++) {
if(box[i][j][k]==1)
queue.offer(new Tomato(k,j,i));
}
}
}
while(!queue.isEmpty()) {
int size = queue.size();
day++;
for(int i=0; i<size;i++) {
Tomato tomato = queue.poll();
for(int j=0; j<6; j++) {
int nx = tomato.x + dx[j];
int ny = tomato.y + dy[j];
int nz = tomato.z + dz[j];
if(nx>=0 && nx<m && ny>=0 && ny<n && nz>=0 && nz<h) {
if(box[nz][ny][nx]==0) {
box[nz][ny][nx] = 1;
queue.offer(new Tomato(nx, ny, nz));
}
}
}
}
}
return day;
}
public static boolean checkTomato() {
for(int i=0; i<h;i++) {
for(int j=0; j<n; j++) {
for(int k=0; k<m; k++) {
if(box[i][j][k]==0)
return false;
}
}
}
return true;
}
}