Skip to content

Commit

Permalink
Merge pull request #52 from AlgoLeadMe/15-mong3125
Browse files Browse the repository at this point in the history
15-mong3125
  • Loading branch information
tgyuuAn authored Jun 27, 2024
2 parents 2a1612d + b53a1de commit de35dc1
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions mong3125/κ·Έλž˜ν”„/BOJ7569_ν† λ§ˆν† .java
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);
}
}

0 comments on commit de35dc1

Please sign in to comment.