알고리즘/그래프
[BOJ] 백준 7579 - 토마토 풀이
송승현(SSH)
2023. 3. 12. 22:32
1. 문제
https://www.acmicpc.net/problem/7569
7569번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,
www.acmicpc.net
2. 풀이 Point
<풀이 Point>
- 입력을 받을 때 처음 익은 토마토의 좌표를 미리 queue에 넣어두고 그곳을 방문처리한다.
- 처음 입력을 받았을 때 모두 익어있을 경우가 있으므로, 이를 탐색한다
- 그 후 BFS 탐색을 진행하면서 토마토가 익는 경우와 익는 day를 갱신한다.
- 탐색을 마쳤을 때 안 익은 토마토가 있는 경우 -1
3. 소스 코드
package bfs;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Tomato2 {
static int M, N, H;
static int[][][] map;
static boolean[][][] visited;
static int[] dx = {1, -1, 0, 0, 0, 0};
static int[] dy = {0, 0, 1, -1, 0, 0};
static int[] dz = {0, 0, 0, 0, 1, -1};
static class tomaotPoint {
int z;
int x;
int y;
int date;
public tomaotPoint(int z, int x, int y, int date) {
this.z = z;
this.x = x;
this.y = y;
this.date = date;
}
}
// 정답 변수
static int result = 0;
// 토마토로 bfs를 돌릴 큐
static Queue<tomaotPoint> tomaotPointQueue = new LinkedList<>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
H = Integer.parseInt(st.nextToken());
map = new int[H][N][M];
visited = new boolean[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++) {
map[i][j][k] = Integer.parseInt(st.nextToken());
// 토마토를 발견하면 큐에 넣어주고 visited 배열을 true
if (map[i][j][k] == 1) {
tomaotPointQueue.add(new tomaotPoint(i, j, k, 0));
visited[i][j][k] = true;
}
}
}
}
// 받은 토마토가 모두 1이거나 토마토가 없는칸 (-1)인경우
boolean flag = true;
for (int i = 0; i < H; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < M; k++) {
if (map[i][j][k] == 0) {
flag = false;
}
}
}
}
if (flag) {
result = 0;
} else {
// 3차원 토마토 bfs 실행
result = tomatoThirdBFS();
// 하나라도 0이 있다면
for (int i = 0; i < H; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < M; k++) {
if (map[i][j][k] == 0) {
result = -1;
}
}
}
}
}
bw.write(Integer.toString(result));
bw.flush();
bw.close();
br.close();
}
private static int tomatoThirdBFS() {
int dayTime = 0;
while (!tomaotPointQueue.isEmpty()) {
tomaotPoint poll = tomaotPointQueue.poll();
// date가 줄어들 일은 없으므로
if (dayTime < poll.date) {
dayTime = poll.date;
}
// z가 있으므로 6번 돌기
for (int i = 0; i < 6; i++) {
int xf = poll.x + dx[i];
int yf = poll.y + dy[i];
int zf = poll.z + dz[i];
if (xf >= 0 && xf < N && yf >= 0 && yf < M && zf >= 0 && zf < H && !visited[zf][xf][yf] && map[zf][xf][yf] != -1) {
visited[zf][xf][yf] = true;
map[zf][xf][yf] = 1;
tomaotPointQueue.add(new tomaotPoint(zf, xf, yf, poll.date + 1));
}
}
}
return dayTime;
}
}
4. 점수
90/100