From 6d7b7d581ea2440da3292f9b4bfa8a90eb193ba0 Mon Sep 17 00:00:00 2001 From: yess98 <52556037+yess98@users.noreply.github.com> Date: Wed, 24 Nov 2021 09:42:54 +0900 Subject: [PATCH] =?UTF-8?q?[=EC=96=91=EC=9D=80=EC=84=9C]=20=ED=86=A0?= =?UTF-8?q?=EB=A7=88=ED=86=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\226\221\354\235\200\354\204\234.java" | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 "solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/\354\226\221\354\235\200\354\204\234.java" diff --git "a/solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/\354\226\221\354\235\200\354\204\234.java" "b/solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/\354\226\221\354\235\200\354\204\234.java" new file mode 100644 index 0000000..eda89f9 --- /dev/null +++ "b/solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/\354\226\221\354\235\200\354\204\234.java" @@ -0,0 +1,77 @@ +``` +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Main { + static int m ; + static int n ; + static int [][] factory; + static int [] dx = {1,-1,0,0}; + static int [] dy = {0,0,1,-1}; + static Queue queue = new ArrayDeque <>(); + static boolean [][] check ; + static int day = -1; + static boolean flag = true; + + 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()); + factory = new int[n][m]; + check = new boolean [n][m]; + for(int i = 0 ; i < n ; i ++) { + st = new StringTokenizer(br.readLine()); + for(int j = 0 ; j < m ; j ++) { + factory[i][j] = Integer.parseInt(st.nextToken()); + if(factory[i][j] == 1) { + queue.add(new int [] {i,j}); + } + } + } + bfs(); + isitdone(); + day = flag == false ? -1 : day -1 ; + System.out.println(day); + + } + static void isitdone() { + for(int i = 0 ; i < n ; i ++) { + for(int j = 0 ; j < m ; j ++) { + if (factory[i][j] == 0) { + flag = false; + break; + } + day = Math.max(day,factory[i][j]); + } + } + } + static void bfs() { + + while (!queue.isEmpty()) { + int [] now = queue.poll(); + + check[now[0]][now[1]] = true; + for(int i = 0 ; i < dx.length; i ++) { + int nx = now[0] + dx[i]; + int ny = now[1] + dy[i]; + if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue; + if (check[nx][ny] == true ) continue; + if (factory[nx][ny] == 0) { + queue.add(new int [] {nx, ny}); + factory[nx][ny] = factory[now[0]][now[1]] + 1; + + } + + } + + } + } +} + +```