Created
August 17, 2020 02:53
-
-
Save niklasjang/e0d7a46ecbf95378cf96a97816a887fb to your computer and use it in GitHub Desktop.
[PS][java][완전탐색][BFS]/[토마토]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.*; | |
public class Main { | |
static int m,n,h; | |
static int[][][] map; | |
static boolean[][][] visit; | |
static Queue<Point> q; | |
static int[] dh = {-1,1,0,0,0,0}; | |
static int[] dn = {0,0,-1,1,0,0}; | |
static int[] dm = {0,0,0,0,-1,1}; | |
static int ans = 0; | |
static int zCnt = 0; | |
public static class Point{ | |
int h,n,m; | |
Point(int h, int n, int m){ | |
this.h = h; | |
this.n = n; | |
this.m = m; | |
} | |
} | |
static boolean check(int ch, int cn, int cm){ | |
if(ch < 0 || h <= ch) return false; | |
if(cn < 0 || n <= cn) return false; | |
if(cm < 0 || m <= cm) return false; | |
return true; | |
} | |
static void bfs(Queue<Point> q){ | |
while(!q.isEmpty() && zCnt > 0){ | |
int size = q.size(); | |
for(int i=0; i< size; i++){ | |
Point curr = q.poll(); | |
for(int k=0; k<6; k++){ | |
int nh = curr.h + dh[k]; | |
int nn = curr.n + dn[k]; | |
int nm = curr.m + dm[k]; | |
if(!check(nh,nn,nm)) continue; | |
if(map[nh][nn][nm] == 0){ | |
zCnt--; | |
map[nh][nn][nm] = 1; | |
visit[nh][nn][nm] = true; | |
q.add(new Point(nh,nn,nm)); | |
} | |
} | |
} | |
ans++; | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); | |
StringTokenizer st = new StringTokenizer(buf.readLine()); | |
m = Integer.parseInt(st.nextToken()); | |
n = Integer.parseInt(st.nextToken()); | |
h = Integer.parseInt(st.nextToken()); | |
map = new int[h][n][m]; | |
visit = new boolean[h][n][m]; | |
q = new LinkedList<>(); | |
q2 = new ArrayDeque<>(); | |
for(int i=0; i<h; i++){ | |
for(int j=0; j<n;j++){ | |
st = new StringTokenizer(buf.readLine()); | |
for(int k=0; k<m;k++){ | |
map[i][j][k] = Integer.parseInt(st.nextToken()); | |
if(map[i][j][k] == 1){ | |
visit[i][j][k] = true; | |
q.add(new Point(i,j,k)); | |
}else if(map[i][j][k] == 0){ | |
zCnt++; | |
} | |
} | |
} | |
} | |
bfs(q); | |
if(zCnt>0){ | |
System.out.println(-1); | |
}else{ | |
System.out.println(ans); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment