Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-mong3125 #52

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ๊ทธ๋ž˜ํ”„;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class BOJ7569_ํ† ๋งˆํ†  {

static int M, N, H;
static int[][][] box;

static Queue<int[]> ripen = new LinkedList<>(); // ์ต์€ ํ† ๋งˆํ†  ์œ„์น˜
static int notRipenTomatos = 0;
static int result = 1;
static int[][] directions = {
{1, 0, 0},
{-1, 0, 0},
{0, 1, 0},
{0, -1, 0},
{0, 0, 1},
{0, 0, -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());

// ์ต์€ ํ† ๋งˆํ†  ์œ„์น˜ ์ €์žฅ
if (box[i][j][k] == 1) ripen.add(new int[]{i, j, k});
else if (box[i][j][k] == 0) notRipenTomatos++;
}
}
}

// == ํ’€์ด == //
bfs();

// ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต์€๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด -1 ๋ฐ˜ํ™˜
if (notRipenTomatos > 0) {
result = 0;
}

// == ์ถœ๋ ฅ == //
System.out.println(result - 1);
}

public static void bfs() {
while (!ripen.isEmpty()) {
int[] tomato = ripen.remove();
for (int[] direction : directions) {
int z = tomato[0] + direction[0];
int y = tomato[1] + direction[1];
int x = tomato[2] + direction[2];

if (isInRange(z, y, x)) {
if (box[z][y][x] == 0) {
box[z][y][x] = box[tomato[0]][tomato[1]][tomato[2]] + 1;
result = Math.max(result, box[z][y][x]);
notRipenTomatos--;

ripen.add(new int[]{z, y, x});
}
}
}
}
}

public static boolean isInRange(int z, int y, int x) {
return (x >= 0 && x < M && y >= 0 && y < N && z >= 0 && z < H);
}
}