1. 程式人生 > 其它 >778. 水位上升的泳池中游泳

778. 水位上升的泳池中游泳

技術標籤:LeetCode刷題

778. 水位上升的泳池中游泳

連結: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) 是連通的

提示:

  1. 2 <= N <= 50.
  2. 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; //出發點就是目標點
    }
};