From 6608275b723d270ec0d9d5cf8923b25b7547a1f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=99=A9=EC=A0=95=ED=9B=88=20=28Hwang=20JungHun=29?= Date: Mon, 20 Dec 2021 16:16:22 +0900 Subject: [PATCH] =?UTF-8?q?Create=20=ED=99=A9=EC=A0=95=ED=9B=88.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\355\231\251\354\240\225\355\233\210.java" | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/\355\231\251\354\240\225\355\233\210.java" diff --git "a/solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/\355\231\251\354\240\225\355\233\210.java" "b/solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/\355\231\251\354\240\225\355\233\210.java" new file mode 100644 index 0000000..77fd4c0 --- /dev/null +++ "b/solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/\355\231\251\354\240\225\355\233\210.java" @@ -0,0 +1,83 @@ +import java.io.*; +import java.util.*; + +public class Main { + + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + + static boolean done = false; + static boolean[][] already; + static int N, M, D = -1; + static int[] dr = {-1, 0, 1, 0}; + static int[] dc = {0, 1, 0, -1}; + static int[][] arr; + static class Pos{ + int r, c; + public Pos(int r, int c){ + this.r = r; + this.c = c; + } + } + static ArrayList pre, post; + + public static void main(String[] args) throws IOException { + + st = new StringTokenizer(br.readLine()); + M = Integer.parseInt(st.nextToken()); + N = Integer.parseInt(st.nextToken()); + + arr = new int[N][M]; + already = new boolean[N][M]; + pre = new ArrayList<>(); + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < M; j++) { + arr[i][j] = Integer.parseInt(st.nextToken()); + if (arr[i][j] == 1) pre.add(new Pos(i, j)); + } + } + + while(!done){ + check(); + flow(); + if(pre.isEmpty() && !done){ + D = -1; + break; + } + D++; + } + bw.write(D+"\n"); + + br.close(); + bw.flush(); + bw.close(); + } + static void check(){ + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (arr[i][j] == 0) return; + } + } + done = true; + } + static void flow(){ + post = new ArrayList<>(); + for(Pos point : pre){ + int r = point.r; + int c = point.c; + already[r][c] = true; + for (int w = 0; w < 4; w++) { + int nr = r + dr[w]; + int nc = c + dc[w]; + if (0<=nr && nr