LeetCode 778. Swim in Rising Water

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].

这题的思路很像Dijkstra's 算法——每一步尽可能选择“最短路径”。

首先,我们思考从a(x0,y0)游到b(x1,y1),假设游到a点花费的时间为t0,那么游到b的时间是多少呢?

t_1 = \begin{cases} t_0& grid[x_1][y_1]<t_0\\ grid[x_1][y_1] & grid[x_1][y_1] > t_0\\ \end{cases} 也即 t_1 = max(& t_0& ,& grid[x_1][y_1]& )

接着,我们要保证每一次游泳花费的时间都是最短的(贪心)。由于t_1 = max(& t_0& ,& grid[x_1][y_1]& ),我们不妨每次都从花费最少的时间的点开始往外游泳。此时当目标点的值比已花费的时间大时,游到目标点的时间为目标点的值(不受影响);而目标点的值比已花费的时间小时,游到目标点的时间为已花费的时间,而已花费的时间是最少时间。

所以最后,我们可以维护一个最小堆,堆顶元素是花费时间最小的点。每次从堆顶的点向外游泳,直到游到终点。

注意,起点的时间也要加上。

class Solution {
public:
    int swimInWater(vector<vector<int>>& grid) {
        int w = grid.size();
        int h = grid[0].size();
        bool reach[grid.size()][grid[0].size()];
        for(int i = grid.size()-1; i >= 0; --i)
            for(int j = grid[0].size()-1; j >= 0; --j){
                reach[i][j]  = false;
            }
        priority_queue<node> heap;
        heap.push(node(grid[0][0])); //起点入堆
        reach[0][0] = true;
        while( !heap.empty() ){
            int x = heap.top().x;
            int y = heap.top().y;
            int time = heap.top().time;
            heap.pop();
            
            if( x > 0 && !reach[x-1][y] ){
                heap.push(node( grid[x-1][y] > time? grid[x-1][y]:time, x-1, y ));
                reach[x-1][y] = true;
            }
            if( x < w - 1 && !reach[x+1][y] ){
                if( x+1 == w - 1 && y == h-1)
                    return grid[x+1][y] > time? grid[x+1][y]:time;
                heap.push(node( grid[x+1][y] > time? grid[x+1][y]:time , x+1, y));
                reach[x+1][y] = true;
            }
            if( y > 0 && !reach[x][y-1] ){
                heap.push(node( grid[x][y-1] > time? grid[x][y-1]:time, x, y-1 ));
                reach[x][y-1] = true;
            }
            if( y < h - 1 && !reach[x][y+1] ){
                if( x == w - 1 && y+1 == h-1 )
                    return grid[x][y+1] > time? grid[x][y+1]:time;
                heap.push(node( grid[x][y+1] > time? grid[x][y+1]:time, x, y+1 ));
                reach[x][y+1] = true;
            }
        }
        return 0;
    }
    
private:
    struct node{
        int time;
        int x;
        int y;
        bool operator<(const node& n) const{
            return time > n.time;
        }
        
        node(int time = 0, int x = 0, int y = 0){
            this->time = time;
            this->x = x;
            this->y = y;
        }
    };
};

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值