From 8d9ad1fe97a73efd71b82341ea38a5ed0208fe57 Mon Sep 17 00:00:00 2001 From: jiminnimij <124450012+jiminnimij@users.noreply.github.com> Date: Sat, 27 Dec 2025 17:38:11 +0900 Subject: [PATCH] =?UTF-8?q?[1227]=202573=EB=B2=88=20-=20=EB=B9=99=EC=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Problem2573.java | 126 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/Problem2573.java diff --git a/src/Problem2573.java b/src/Problem2573.java new file mode 100644 index 0000000..4719897 --- /dev/null +++ b/src/Problem2573.java @@ -0,0 +1,126 @@ +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.IOException; +import java.util.StringTokenizer; +import java.util.Queue; +import java.util.LinkedList; + +public class Problem2573 { + static int[] DN = {1, -1, 0, 0}; + static int[] DM = {0, 0, 1, -1}; + static Queue melt = new LinkedList<>(); + static Queue count = new LinkedList<>(); + + static int sum = 0; + static int time = 0; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + + int[][] iceburg = new int[n][m]; + + for (int i = 0; i < n; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < m; j++) { + iceburg[i][j] = Integer.parseInt(st.nextToken()); + } + } + + while(true) { + // 덩어리 개수 세기 + int iceburgCount = countIceburg(iceburg, n, m); + + if (iceburgCount > 1) { + break; + } + + if (melt.isEmpty()) { + time = 0; + break; + } + + // 멜팅 로직 + meltIceburg(iceburg, n, m); + time++; + + + } + bw.write(String.valueOf(time)); + bw.flush(); + bw.close(); + } + + public static int countIceburg(int[][] iceburg, int n, int m) { + sum = 0; + + boolean[][] visited = new boolean[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + visited[i][j] = false; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (iceburg[i][j] > 0 && !visited[i][j]) { + sum++; + visited[i][j] = true; + + count.add(new int[]{i, j}); + + while (!count.isEmpty()) { + int[] cur = count.poll(); + int x = cur[0]; + int y = cur[1]; + int meltCount = 0; + + for (int k = 0; k < 4; k++) { + int nx = x + DN[k]; + int ny = y + DM[k]; + + if (nx >= 0 && nx < n && ny >= 0 && ny < m) { + if (iceburg[nx][ny] == 0) { + meltCount++; + } + if (iceburg[nx][ny] > 0 && !visited[nx][ny]) { + visited[nx][ny] = true; + count.add(new int[]{nx, ny}); + } + } + } + melt.add(new int[]{x, y, meltCount, time}); + } + } + } + } + + return sum; + } + + public static void meltIceburg(int[][] iceburg, int n, int m) { + while (!melt.isEmpty()) { + int[] cur = melt.peek(); + if (cur[3] == time) { + melt.poll(); + int x = cur[0]; + int y = cur[1]; + int meltCount = cur[2]; + + iceburg[x][y] -= meltCount; + if (iceburg[x][y] < 0) { + iceburg[x][y] = 0; + } + } + else { + break; + } + } + } +} \ No newline at end of file