Repository navigation
Expand file tree
/
Copy pathCode03_SwimInRisingWater.java
More file actions
66 lines (61 loc) · 2.05 KB
/
Copy pathCode03_SwimInRisingWater.java
File metadata and controls
66 lines (61 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package class064;
import java.util.PriorityQueue;
// 水位上升的泳池中游泳
// 在一个 n x n 的整数矩阵 grid 中
// 每一个方格的值 grid[i][j] 表示位置 (i, j) 的平台高度
// 当开始下雨时,在时间为 t 时,水池中的水位为 t
// 你可以从一个平台游向四周相邻的任意一个平台,但是前提是此时水位必须同时淹没这两个平台
// 假定你可以瞬间移动无限距离,也就是默认在方格内部游动是不耗时的
// 当然,在你游泳的时候你必须待在坐标方格里面。
// 你从坐标方格的左上平台 (0,0) 出发
// 返回 你到达坐标方格的右下平台 (n-1, n-1) 所需的最少时间
// 测试链接 : https://leetcode.cn/problems/swim-in-rising-water/
public class Code03_SwimInRisingWater {
// 0:上,1:右,2:下,3:左
public static int[] move = new int[] { -1, 0, 1, 0, -1 };
public static int swimInWater(int[][] grid) {
int n = grid.length;
int m = grid[0].length;
int[][] distance = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
distance[i][j] = Integer.MAX_VALUE;
}
}
distance[0][0] = grid[0][0];
boolean[][] visited = new boolean[n][m];
// 0 : 格子的行
// 1 : 格子的列
// 2 : 源点到当前格子的代价
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[2] - b[2]);
heap.add(new int[] { 0, 0, grid[0][0] });
while (!heap.isEmpty()) {
int x = heap.peek()[0];
int y = heap.peek()[1];
int c = heap.peek()[2];
heap.poll();
if (visited[x][y]) {
continue;
}
visited[x][y] = true;
if (x == n - 1 && y == m - 1) {
// 常见剪枝
// 发现终点直接返回
// 不用等都结束
return c;
}
for (int i = 0, nx, ny, nc; i < 4; i++) {
nx = x + move[i];
ny = y + move[i + 1];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny]) {
nc = Math.max(c, grid[nx][ny]);
if (nc < distance[nx][ny]) {
distance[nx][ny] = nc;
heap.add(new int[] { nx, ny, nc });
}
}
}
}
return -1;
}
}