From d013613401d0d8c15fe00d3d3b5fd83748190988 Mon Sep 17 00:00:00 2001 From: yukyeongmin <75004920+yukyeongmin@users.noreply.github.com> Date: Fri, 26 Nov 2021 16:46:58 +0900 Subject: [PATCH] =?UTF-8?q?[=EC=9C=A0=EA=B2=BD=EB=AF=BC]=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 생각보다 공간 차지를 많이 하네요 --- .../ykm.java" | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 "solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/ykm.java" diff --git "a/solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/ykm.java" "b/solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/ykm.java" new file mode 100644 index 0000000..6599a19 --- /dev/null +++ "b/solution/BOJ_7576_\355\206\240\353\247\210\355\206\240/ykm.java" @@ -0,0 +1,77 @@ + +import java.io.*; +import java.util.*; + +public class Main { + static int map[][]; + static int row; + static int col; + + static Queue q = new LinkedList(); + static int[] dx = { -1, 1, 0, 0 }; + static int[] dy = { 0, 0, -1, 1 }; + + static int area = 0; // 익혀야 되는 토마토의 개수 + + public static void main(String[] args) throws IOException { + System.setIn(new FileInputStream("input.txt")); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + col = Integer.parseInt(st.nextToken()); + row = Integer.parseInt(st.nextToken()); + map = new int[row][col]; + + + for (int i = 0; i < row; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < col; j++) { + map[i][j] = Integer.parseInt(st.nextToken()); + if (map[i][j] == 1) { + q.add(new int[] { i, j, 1 }); + } else if (map[i][j] == 0) { + area++; + } + } + } + + if (area == 0) { + System.out.println(0); + } else { + int turn = spread(); + + if (area != 0) + System.out.println(-1); + else + System.out.println(turn); + + } + + br.close(); + + } + + private static int spread() { + int turn = 0; + + while (!q.isEmpty()) { + int[] current = q.poll(); + + for (int i = 0; i < 4; i++) { + int nextX = current[0] + dx[i]; + int nextY = current[1] + dy[i]; + + if (nextX < 0 || nextY < 0 || nextX >= row || nextY >= col) + continue; + + if (map[nextX][nextY] == 0) { + map[nextX][nextY] = current[2] + 1; + turn = Math.max(turn, map[nextX][nextY]); + q.add(new int[] { nextX, nextY, current[2] + 1 }); + area --; + } + } + } + + return turn - 1 ; + } +}