链接:https://leetcode-cn.com/problems/swim-in-rising-water/
在一个 N x N 的坐标方格 grid
中,每一个方格的值 grid[i][j]
表示在位置 (i,j)
的平台高度。
现在开始下雨了。当时间为 t
时,此时雨水导致水池中任意位置的水位为 t
。你可以从一个平台游向四周相邻的任意一个平台,但是前提是此时水位必须同时淹没这两个平台。假定你可以瞬间移动无限距离,也就是默认在方格内部游动是不耗时的。当然,在你游泳的时候你必须待在坐标方格里面。
你从坐标方格的左上平台 (0,0) 出发。最少耗时多久你才能到达坐标方格的右下平台 (N-1, N-1)
?
示例 1:
输入: [[0,2],[1,3]]
输出: 3
解释:
时间为0时,你位于坐标方格的位置为 (0, 0)。
此时你不能游向任意方向,因为四个相邻方向平台的高度都大于当前时间为 0 时的水位。
等时间到达 3 时,你才可以游向平台 (1, 1). 因为此时的水位是 3,坐标方格中的平台没有比水位 3 更高的,所以你可以游向坐标方格中的任意位置
示例2:
输入: [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]] 输出: 16 解释: 0 1 2 3 4 24 23 22 21 5 12 13 14 15 16 11 17 18 19 20 10 9 8 7 6 最终的路线用加粗进行了标记。 我们必须等到时间为 16,此时才能保证平台 (0, 0) 和 (4, 4) 是连通的
提示:
2 <= N <= 50
.grid[i][j]
是[0, ..., N*N - 1]
的排列。
思路:分析一下,当时刻为t的时候,所有格子中的水位是一样高的,所以最低的水位就是路径上的最大值,我们要让这个最大值最小,和昨天那道 1631. 最小体力消耗路径 一样的思路啊喂:https://blog.csdn.net/qq_39304630/article/details/113364066。
A. 二分答案 + 连通性判定(BFS or DFS)
class Solution {
public:
bool visited[55][55];
struct Node{
int x, y;
};
bool BFS(int n, int m, vector<vector<int>>& grid, int limit){
memset(visited, 0, sizeof(visited));
int x, y;
queue<Node> que;
que.push(Node{0, 0});
visited[0][0] = true;
Node node;
while(!que.empty()){
node = que.front();
que.pop();
x = node.x, y = node.y;
if(x == n - 1 && y == m - 1){
return true;
}
if(x > 0 && !visited[x - 1][y] && grid[x - 1][y] <= limit){
visited[x - 1][y] = true;
que.push(Node{x - 1, y});
}
if(x + 1 < n && !visited[x + 1][y] && grid[x + 1][y] <= limit){
visited[x + 1][y] = true;
que.push(Node{x + 1, y});
}
if(y > 0 && !visited[x][y - 1] && grid[x][y - 1] <= limit){
visited[x][y - 1] = true;
que.push(Node{x, y - 1});
}
if(y + 1 < m && !visited[x][y + 1] && grid[x][y + 1] <= limit){
visited[x][y + 1] = true;
que.push(Node{x, y + 1});
}
}
return false;
}
int swimInWater(vector<vector<int>>& grid) {
int L = grid[0][0], R = 2499, M, i, j, n = grid.size(), m = grid[0].size(), ans = 0;
while(L <= R){
M = (L + R) >> 1;
if(BFS(n, m, grid, M)){
ans = M;
R = M - 1;
}else{
L = M + 1;
}
}
return ans;
}
};
B. 并查集+贪心
class Solution {
public:
int Father[2510], Index[55][55];
int Find(int x){
return x == Father[x] ? x : Father[x] = Find(Father[x]);
}
void Union(int A, int B){
A = Find(A);
B = Find(B);
if(A != B){
Father[A] = B;
}
}
int inSet[55][55];
struct Point{
int x, y, w;
bool operator < (const Point p) const{
return w < p.w;
}
};
Point points[2510];
int swimInWater(vector<vector<int>>& grid) {
memset(inSet, false, sizeof(inSet));
int n = grid.size(), i, j, cnt = 0, x, y, w, index, s = 0, o = n * n -1;
for(i = 0; i < n; ++ i){
for(j = 0; j < n; ++ j){
Index[i][j] = index = i * n + j;
Father[index] = index;
points[cnt ++] = Point{i, j, grid[i][j]};
}
}
sort(points, points + cnt);
for(i = 0; i < cnt; ++ i){
x = points[i].x, y = points[i].y, w = points[i].w;
inSet[x][y] = true;
index = Index[x][y];
if(x > 0 && inSet[x - 1][y]){
Union(index, Index[x - 1][y]);
if(Find(s) == Find(o)) return w;
}
if(x + 1 < n && inSet[x + 1][y]){
Union(index, Index[x + 1][y]);
if(Find(s) == Find(o)) return w;
}
if(y > 0 && inSet[x][y - 1]){
Union(index, Index[x][y - 1]);
if(Find(s) == Find(o)) return w;
}
if(y + 1 < n && inSet[x][y + 1]){
Union(index, Index[x][y + 1]);
if(Find(s) == Find(o)) return w;
}
}
return 0; //出发点就是目标点
}
};
C. Djkstra求最短路
class Solution {
public:
int dist[55][55];
bool visited[55][55];
struct Point{
int x, y, w;
bool operator < (const Point p) const{ //改大根堆为小根堆
return w > p.w;
}
};
int swimInWater(vector<vector<int>>& grid) {
memset(visited, false, sizeof(visited));
int n = grid.size(), i, j, x, y, w, s = 0, o = n * n -1;
priority_queue<Point> pri_que;
pri_que.push(Point{0, 0, grid[0][0]});
Point point;
while(!pri_que.empty()){
auto [x, y, w] = pri_que.top();
pri_que.pop();
visited[x][y] = true;
// 到终点
if(x == n - 1 && y == n -1){
return w;
}
//没到终点 更新dist
if(x > 0 && !visited[x - 1][y]){
pri_que.push(Point{x - 1, y, max(w,grid[x - 1][y])});
}
if(x + 1 < n && !visited[x + 1][y]){
pri_que.push(Point{x + 1, y, max(w,grid[x + 1][y])});
}
if(y > 0 && !visited[x][y - 1]){
pri_que.push(Point{x, y - 1, max(w,grid[x][y - 1])});
}
if(y + 1 < n && !visited[x][y + 1]){
pri_que.push(Point{x, y + 1, max(w,grid[x][y + 1])});
}
}
return 0; //出发点就是目标点
}
};