ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode-778.Swim in Rising Water(水位上升的泳池中游泳)

2021-01-31 19:32:03  阅读:200  来源: 互联网

标签:Swim parent 16 int 778 grid 方格 time 泳池


水位上升的泳池中游泳

在一个 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] 的排列

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

方法一:并查集
这个方法应该是比较容易想到和实现的了。
我们模拟下雨的过程,在时刻t,对于高度为t单元格,我们检查当前时刻是否能够游到相邻的单元格内(即高度小于等于当前单元格的高度),然后将能够到达的单元格合并。当终点和起点连通时,就是我们要求的时间。

class Solution {
   public:
    int n;
    int swimInWater(vector<vector<int>>& grid) {
        int directions[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        n = grid.size();
        int len = n * n;
		vector<int> index(len);
		for(int i=0;i<n;++i){
			for(int j=0;j<n;++j){
				index[grid[i][j]] = getIndex(i,j);
			}
		}

		UnionFind unionFind(len);
		for(int i=0;i<len;++i){
			int x = index[i]/n;
			int y = index[i]%n;

			for(auto direction:directions){
				int newX = x + direction[0];
				int newY = y + direction[1];
				if(isIn(newX,newY) && grid[newX][newY] <= i){
					unionFind.connect(index[i], getIndex(newX,newY));
				}
				if(unionFind.isConnected(0,len-1)){
					return i;
				}
			}
		}
		return -1;
    }

   private:
    int getIndex(int x, int y) { return x * n + y; }
    bool isIn(int x, int y) { return x >= 0 && x < n && y >= 0 && y < n; }
    class UnionFind {
       private:
        vector<int> parent;

       public:
        UnionFind(int n) : parent(n) {
            for (int i = 0; i < n; ++i) {
                parent[i] = i;
            }
        }
        int root(int x) {
            while (x != parent[x]) {
                parent[x] = parent[parent[x]];
                x = parent[x];
            }
            return x;
        }
        bool isConnected(int x, int y) { return root(x) == root(y); }
        void connect(int p, int q) {
            if (isConnected(p, q)) {
                return;
            }
            parent[root(p)] = root(q);
        }
    };
};

另外本题还有搜索算法和Dijkstra算法等方法,之后更新...

标签:Swim,parent,16,int,778,grid,方格,time,泳池
来源: https://www.cnblogs.com/qdcs2000/p/14353645.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有