|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + static int n, m; |
| 9 | + static int[][] map, meltcount; |
| 10 | + static boolean[][] visited; |
| 11 | + static int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; |
| 12 | + |
| 13 | + public static void main(String[] args) throws Exception { |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 16 | + |
| 17 | + n = Integer.parseInt(st.nextToken()); |
| 18 | + m = Integer.parseInt(st.nextToken()); |
| 19 | + |
| 20 | + map = new int[n][m]; |
| 21 | + visited = new boolean[n][m]; |
| 22 | + |
| 23 | + for (int i = 0; i < n; i++) { |
| 24 | + st = new StringTokenizer(br.readLine()); |
| 25 | + for (int j = 0; j < m; j++) { |
| 26 | + int num = Integer.parseInt(st.nextToken()); |
| 27 | + map[i][j] = num; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + int year = 0; |
| 32 | + while (true) { |
| 33 | + int count = countice(); |
| 34 | + |
| 35 | + if (count >= 2) { |
| 36 | + System.out.println(year); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (count == 0) { |
| 41 | + System.out.println(0); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + melt(); |
| 46 | + year++; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + static void bfs(int x, int y) { |
| 51 | + Queue<int[]> q = new LinkedList<>(); |
| 52 | + q.offer(new int[]{x, y}); |
| 53 | + visited[x][y] = true; |
| 54 | + |
| 55 | + while (!q.isEmpty()) { |
| 56 | + int[] now = q.poll(); |
| 57 | + for (int d = 0; d < 4; d++) { |
| 58 | + int dy = now[0] + dir[d][0]; |
| 59 | + int dx = now[1] + dir[d][1]; |
| 60 | + |
| 61 | + if (dy >= 0 && dx >= 0 && dy < n && dx < m) { |
| 62 | + if (map[dy][dx] > 0 && !visited[dy][dx]) { |
| 63 | + visited[dy][dx] = true; |
| 64 | + q.offer(new int[]{dy, dx}); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + static int countice() { |
| 72 | + visited = new boolean[n][m]; |
| 73 | + int count = 0; |
| 74 | + |
| 75 | + for (int i = 0; i < n; i++) { |
| 76 | + for (int j = 0; j < m; j++) { |
| 77 | + if (map[i][j] > 0 && !visited[i][j]) { |
| 78 | + bfs(i, j); |
| 79 | + count++; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return count; |
| 84 | + } |
| 85 | + |
| 86 | + static void melt() { |
| 87 | + meltcount = new int[n][m]; |
| 88 | + |
| 89 | + for (int i = 0; i < n; i++) { |
| 90 | + for (int j = 0; j < m; j++) { |
| 91 | + if (map[i][j] > 0) { |
| 92 | + int sea = 0; |
| 93 | + for (int d = 0; d < 4; d++) { |
| 94 | + int dy = i + dir[d][0]; |
| 95 | + int dx = j + dir[d][1]; |
| 96 | + if (dy >= 0 && dx >= 0 && dy < n && dx < m) { |
| 97 | + if (map[dy][dx] == 0) |
| 98 | + sea++; |
| 99 | + } |
| 100 | + } |
| 101 | + meltcount[i][j] = sea; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + for (int i = 0; i < n; i++) { |
| 107 | + for (int j = 0; j < m; j++) { |
| 108 | + map[i][j] -= meltcount[i][j]; |
| 109 | + if (map[i][j] < 0) |
| 110 | + map[i][j] = 0; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
0 commit comments