Skip to content

Latest commit

 

History

History
159 lines (93 loc) · 3.99 KB

File metadata and controls

159 lines (93 loc) · 3.99 KB

中文文档

Description

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

Solutions

Python3

Java

class Solution {
    // x、y方向向量
    public static final int[] dx = {0, 0, 1, -1};
    public static final int[] dy = {1, -1, 0, 0};
    /**
     * https://blog.csdn.net/fuxuemingzhu/article/details/82926674
     * <p>
     * 参考这篇文章的第二种解题方法做的
     * <p>
     * 通过优先级队列找寻局部最优解  最终的得到的结果就是全局最优解
     *
     * @param grid
     * @return
     */
    // 以grid左上角为原点,横向为X轴,纵向为Y轴
    public int swimInWater(int[][] grid) {
        // 定义一个优先级队列  按照h从小到大排列
        Queue<Pair<Integer, Pair<Integer, Integer>>> queue = new PriorityQueue<>(Comparator.comparing(Pair::getKey));
        queue.add(new Pair<>(grid[0][0], new Pair<>(0, 0)));
        // 已经遍历过的点
        Set<Pair<Integer, Integer>> visitSet = new HashSet<>();
        visitSet.add(new Pair<>(0, 0));

        int res = 0;
        int length = grid.length;

        while (!queue.isEmpty()) {
            Pair<Integer, Pair<Integer, Integer>> top = queue.poll();
            Integer x = top.getValue().getKey();
            Integer y = top.getValue().getValue();
            res = Math.max(res, top.getKey());
            // 2 <= N <= 50 这个范围内可以直接使用==进行Integer的比较
            if (x == top.getValue().getValue() && y == length - 1) {
                break;
            }

            for (int i = 0; i < 4; i++) {
                int newY = y + dy[i];
                int newX = x + dx[i];
                if (newX < 0 || newY < 0 || newX >= length || newY >= length || visitSet.contains(new Pair<>(newX, newY))) {
                    // 直接忽略
                    continue;
                }
                queue.add(new Pair<>(grid[newX][newY], new Pair<>(newX, newY)));
                visitSet.add(new Pair<>(newX, newY));
            }
        }
        return res;
    }
}

...