CodingTest/Java
BOJ_7569 : ν λ§ν
Soom_1n
2023. 2. 3. 09:15
7569λ²: ν λ§ν
첫 μ€μλ μμμ ν¬κΈ°λ₯Ό λνλ΄λ λ μ μ M,Nκ³Ό μμμ¬λ €μ§λ μμμ μλ₯Ό λνλ΄λ Hκ° μ£Όμ΄μ§λ€. Mμ μμμ κ°λ‘ μΉΈμ μ, Nμ μμμ μΈλ‘ μΉΈμ μλ₯Ό λνλΈλ€. λ¨, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,
www.acmicpc.net
πΈ λ¬Έμ λΆμ πΈ
- H x N x M ν¬κΈ°μ μ°½κ³ μ μ΅κ±°λ λ μ΅μ ν λ§ν κ° λ€μ΄μλ€.
- μ΅μ ν λ§ν λ μνμ’μ°μ νμ λ μ΅μ ν λ§ν λ₯Ό λ€μ λ μ΅κ² νλ€.
- λΉ μΉΈλ μλ€.
- ν λ§ν κ° λͺ¨λ μ΅μ λ κΉμ§ μ΅μ λ©°μΉ μ΄ κ±Έλ¦¬λμ§ μΆλ ₯νλ€.
- λͺ¨λ μ΅μ§ λͺ»νλ©΄ -1μ μΆλ ₯νλ€.
- BFSλ₯Ό ν΅ν΄ μ΅μ ν λ§ν μ λ μ΅μ ν λ§ν λ€μ΄ λͺ¨λ μ΅μ λ κΉμ§ 걸리λ λ μ κ³μ°νλ€.
- νμμ λ°©κΈ μ΅μ ν λ§ν μ μ’νλ₯Ό κΊΌλΈλ€.
- κΊΌλΈ ν λ§ν μ£Όλ³μ λ μ΅μ ν λ§ν μ κ°μ νμ¬ κ°+1 λ‘ λ°κΎΈκ³ , μ’νλ₯Ό νμ λ£λλ€.
- νκ° λΉ λ κΉμ§ λ°λ³΅νλ€.
- ν λ§ν μ°½κ³ μμ κ°μ₯ ν° κ°μ μΆλ ₯νλ€.
- 0μ΄ μμΌλ©΄ -1μ λ°ννλ€. (λͺ¨λ μ΅μ μ μμ)
πΈ μ½λ πΈ
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// input
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int h = sc.nextInt();
int[][][] arr = new int[h][n][m];
Queue<Tomato> que = new LinkedList<>();
for (int i = 0; i < h; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
arr[i][j][k] = sc.nextInt();
if (arr[i][j][k] == 1)
que.add(new Tomato(j, k, i));
}
}
}
// BFS
// μ μ° ν μ’ μ ν
int[] dx = {-1, 0, 1, 0, 0, 0};
int[] dy = {0, 1, 0, -1, 0, 0};
int[] dz = {0, 0, 0, 0, 1, -1};
while(!que.isEmpty()) {
Tomato tomato = que.poll();
// System.out.println(tomato.h + " " + tomato.n + " " + tomato.m);
// print(arr);
for (int i = 0; i < dx.length; i++) {
int x = tomato.n + dx[i];
int y = tomato.m + dy[i];
int z = tomato.h + dz[i];
if (0 <= x && x < n &&
0 <= y && y < m &&
0 <= z && z < h &&
arr[z][x][y] == 0) {
arr[z][x][y] = arr[tomato.h][tomato.n][tomato.m] + 1;
que.add(new Tomato(x,y,z));
}
}
}
// print(arr);
System.out.println(tomato(arr));
}
private static int tomato(int[][][] arr) {
int max = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
for (int k = 0; k < arr[i][j].length; k++) {
if (arr[i][j][k] == 0)
return -1;
if (arr[i][j][k] > max)
max = arr[i][j][k];
};
}
}
return max-1;
}
private static class Tomato {
int n, m, h;
public Tomato(int n, int m, int h) {
this.n = n;
this.m = m;
this.h = h;
}
}
private static void print(int[][][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
for (int k = 0; k < arr[i][j].length; k++) {
System.out.print(arr[i][j][k] + " ");
}
System.out.println();
}
System.out.println("\n----------------------------------------\n");
}
System.out.println("\n==========================================\n");
}
}
πΈ μ½λ ν΄μ πΈ
- μ°½κ³ μ λμ΄ h, μΈλ‘ λλΉ n, κ°λ‘ λλΉ hλ₯Ό μ
λ ₯λ°κ³ μ°½κ³ λ°°μ΄ arrμ κ°μ μ μ₯νλ€.
- νλ₯Ό λ§λ€μ΄μ arrμ κ°μ λ£μ λ 1μΈ μ’νλ₯Ό μ΄κΈ°κ°μΌλ‘ λ£μ΄μ€λ€.
- μ’νλ₯Ό μ μ₯νκΈ° μν΄ Tomato ν΄λμ€λ₯Ό λ§λ€μ΄ h, n, m κ°μ μμ±μΌλ‘ μ μ₯νλ€.
- BFSλ₯Ό μννλ€.
- μ’ν μ΄λμ μν΄ μ μ’νμ°μν λ°©ν₯μ h,n,m μΈλ±μ€ λ³ν λ°°μ΄ dx, dy, dzλ₯Ό μ΄κΈ°ν νλ€.
- νκ° λΉ λκΉμ§ κ³μ°μ λ°λ³΅νλ€.
- νμμ Tomato κ°μ²΄λ₯Ό νλ poll() νλ€.
- 6λ°©ν₯μ h, m, n μ΄λμ νμΈν΄μ μ°½κ³ λ²μ λ΄μ΄λ©° κ°μ΄ 0μ΄λ©΄ νμ¬ κ°+1μ μ μ₯νκ³ ν΄λΉ μ’νλ₯Ό νμ μ μ₯νλ€.
- arrμ 0μ΄ λ¨μμμΌλ©΄ -1μ μΆλ ₯νκ³ , μλ€λ©΄ arrμ μμ μ€ μ΅λκ°-1μ μΆλ ₯νλ€.
πΈ end πΈ
- μ΄μ ν λ§ν λ νλ₯Ό 2κ°λ₯Ό μ¬μ©ν΄ 볡μ¬νλ©° νμ΄νλλ°, arrμ κ°μ λμ νλ λ°©μμ νμ΄λ₯Ό λ³΄κ³ μ΄λ²μ μ μ©ν΄λ³΄μλ€.
- BFSμμ μ¬μ©νκΈ° μμ£Ό μ’μ λ°©λ²μΈ κ² κ°λ€.
- printλ©μλλ λ³΄ν΅ μ§μ°λλ°, μ΄λ²μ μλ κ·Έλλ‘ μ¬λ Έλ€.
- μκ°λ³΄λ€ λμμ§ μμ κ² κ°μμ μ½λμ λ¨κ²¨λλ €κ³ νλ€.
- κ°μ μκ³ λ¦¬μ¦μ λ¬Έμ λ₯Ό νμλλ μ μ κ²λ€μ΄ λ§μ§ μμλ€.
- κ³μ κ°λ¨ν μΈ μ μλλ‘ μ°μ΅ν μκ°μ΄λ€.
728x90