[Leetcode] 778. Swim in Rising Water 解题报告

本文介绍了一种结合二分查找与深度优先搜索(DFS)的算法,用于解决特定类型的路径寻找问题。该算法的目标是在一个给定的N×N网格中找出从左上角到右下角所需的最短时间,考虑到每个网格位置的高度限制以及随时间增加的水面高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j).

Now rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most t. You can swim infinite distance in zero time. Of course, you must stay within the boundaries of the grid during your swim.

You start at the top left square (0, 0). What is the least time until you can reach the bottom right square (N-1, N-1)?

Example 1:

Input: [[0,2],[1,3]]
Output: 3
Explanation:
At time 0, you are in grid location (0, 0).
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.

You cannot reach point (1, 1) until time 3.
When the depth of water is 3, we can swim anywhere inside the grid.

Example 2:

Input: [[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]]
Output: 16
Explanation:
 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

The final route is marked in bold.
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.

Note:

  1. 2 <= N <= 50.
  2. grid[i][j] is a permutation of [0, ..., N*N - 1].

思路

一道二分查找和DFS相结合的题目,个人觉得是一道不错的面试题。通过抽茧剥丝,我们发现这道题目其实就是求从grid的左上角到右下角的所有路径中,最大值最小的路径中的最大值(原谅我说的有点绕^_^)。

我们可以对该值进行二分查找:该值的最小可能取值就是grid中的最小值,最大可能取值就是grid中的最大值。我们分别定义它们为left和right。然后就二分查找left和right中的合适值,使得从grid的左上角到右下角有路径可达且阈值最小。在针对mid做一次DFS之后,可能会出现两种情况:

1)以mid为阈值无法从左上角到右下角,所以此时我们需要提高阈值,即让left = mid + 1;

2)以mid为阈值可以从左上角到右下角,所以此时我们可以降低阈值,即让right = mid - 1,以便于找到更小的阈值。

最终返回left即可。

代码

class Solution {
public:
    int swimInWater(vector<vector<int>>& grid) {
        int right = INT_MIN, left = INT_MAX;
        int row_num = grid.size(), col_num = grid[0].size();
        for (int r = 0; r < row_num; ++r) {
            for (int c = 0; c < col_num; ++c) {
                right = max(right, grid[r][c]);
                left = min(left, grid[r][c]);
            }
        }
        while (left <= right) {
            int mid = left + (right - left) / 2;
            vector<vector<bool>> visited(row_num, vector<bool>(col_num, false));
            DFS(grid, visited, 0, 0, mid);
            if (!visited[row_num - 1][col_num - 1]) {
                left = mid + 1;
            }
            else {
                right = mid - 1;
            }
        }
        return left;
    }
private:
    void DFS(vector<vector<int>> &grid, vector<vector<bool>> &visited, int row, int col, int threshold) {
        int row_num = grid.size(), col_num = grid[0].size();
        if (row < 0 || row >= row_num || col < 0 || col >= col_num ||
            visited[row][col] || grid[row][col] > threshold) {
            return;
        }
        visited[row][col] = true;
        DFS(grid, visited, row - 1, col, threshold);
        DFS(grid, visited, row + 1, col, threshold);
        DFS(grid, visited, row, col - 1, threshold);
        DFS(grid, visited, row, col + 1, threshold);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值