-
[BOJ - JAVA] 7569 - 토마토(BFS) 본문
728x90
반응형
# 주소
https://www.acmicpc.net/problem/7569
# 문제
# 문제 해설 및 코드 리뷰
import java.util.*;
class Point{
int x;
int y;
int z;
Point(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}
public class Main{
public static int[] dx = {1,0,0,-1,0,0};
public static int[] dy = {0,1,-1,0,0,0};
public static int[] dz = {0,0,0,0,-1,1};
public static int width, height, h;
public static int count;
public static boolean visit[][][];
public static int arr[][][];
public static Queue<Point> queue = new LinkedList<>();
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
height = scan.nextInt();
width = scan.nextInt();
h = scan.nextInt();
count = 0;
arr = new int[h][width][height];
visit = new boolean[h][width][height];
for(int k = 0; k < h; k++){
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
arr[k][i][j] = scan.nextInt();
if(arr[k][i][j] == 1){
queue.offer(new Point(k,i,j));
visit[k][i][j] = true;
}
}
}
}
while(!queue.isEmpty()){
Point now = queue.poll();
int x = now.x;
int y = now.y;
int z = now.z;
for(int i = 0; i < 6; i++){
int nx = x + dx[i];
int ny = y + dy[i];
int nz = z + dz[i];
if(0 <= nx && nx < h && 0 <= ny && ny < width && 0 <= nz && nz < height){
if(arr[nx][ny][nz] == 0 && visit[nx][ny][nz] == false){
queue.offer(new Point(nx,ny,nz));
arr[nx][ny][nz] = arr[x][y][z] + 1;
}
}
}
}
for(int k = 0; k < h; k++){
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
if(arr[k][i][j] == 0){
System.out.println(-1);
return;
}
if(count < arr[k][i][j]){
count = arr[k][i][j];
}
}
}
}
System.out.println(count-1);
}
}
이전 문제랑 너무 똑같은 문제죠??? 이번엔 3차원 배열을 해야했기 때문에 생각할 것이 더 많았습니다.
아무래도 가야할 방향이 상하좌우만 있는 것이 아니라 앞과 뒤도 있기 때문입니다.
그 부분만 신경 써주신다면
https://codingrapper.tistory.com/95
이 문제랑 완전 똑같은 문제입니다.
너무 간단한거라 넘어갈게요. BFS도 슬슬 질리네요 ㅠㅠㅠ
728x90
반응형
'백준 문제 풀이' 카테고리의 다른 글
[BOJ - JAVA] 1021 - 회전하는 큐 (덱) (0) | 2021.11.28 |
---|---|
[BOJ - JAVA] 1697 - 숨바꼭질(BFS) (0) | 2021.11.26 |
[BOJ - JAVA] 7576 - 토마토(BFS) (0) | 2021.11.24 |
[BOJ - JAVA] 7562 - 나이트의 이동(BFS) (0) | 2021.11.23 |
[BOJ - JAVA] 1012 - 유기농 배추(BFS) (0) | 2021.11.22 |
Comments