LeetCode破解之泳池问题

「这是我参与11月更文挑战的第18天,活动详情查看:2021最后一次更文挑战

题目描述

在一个 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 更高的,所以你可以游向坐标方格中的任意位置

方法1:迪杰特斯拉求最短路径

分析题意,可以发现其实就是dijkstra算法的板子题,如果当前格子比邻居格子高,则连接边权重0,否则权重为邻居格子和当前格子的差(非负数)。求起点到终点最短路径。

class Solution {
    public int swimInWater(int[][] grid) {
        int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        int m = grid.length, n = grid[0].length;
        PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() {
            public int compare(int[] edge1, int[] edge2) {
                return edge1[2] - edge2[2];
            }
        });
        pq.offer(new int[]{0, 0, grid[0][0]});
        int[] dist = new int[m * n];
        Arrays.fill(dist, Integer.MAX_VALUE);
        dist[0] = grid[0][0];
        boolean[] seen = new boolean[m * n];

        while (!pq.isEmpty()) {
            int[] edge = pq.poll();
            int x = edge[0], y = edge[1], d = edge[2];
            int id = x * n + y;
            if (seen[id]) {
                continue;
            }
            if (x == m - 1 && y == n - 1) {
                break;
            }
            seen[id] = true;
            for (int i = 0; i < 4; ++i) {
                int nx = x + dir[i][0], ny = y + dir[i][1];
                if (nx >= 0 && nx < m && ny >= 0 && ny < n) {
                    int temp = Math.max(d, grid[nx][ny]);
                    if (temp < dist[nx * n + ny]) {
                        dist[nx * n + ny] = temp;
                        pq.offer(new int[]{nx, ny, dist[nx * n + ny]});
                    }
                } 
            }
        }
        return dist[m * n - 1];
    }
}
复制代码

方法2:DFS+二分

分析:我们可以发现如果从时间t=0开始,标记所有在时刻t起点[0,0]能到达的点 (grid[i][j] <= t),直到在某个时刻t‘能标记到终点[N-1][N-1]时结束,t’就是所需的最短时间。

    class Solution {
    int[][] directions = new int[][]{{0,1}, {0,-1}, {1,0}, {-1,0}};
    int rows;
    public int swimInWater(int[][] grid) {
        rows = grid.length;
        int left = 0, right = rows * rows;
        while(left <= right){

            int mid = (left + right) / 2;
            if(canGet(mid, grid, new boolean[rows][rows], 0, 0))
                right = mid - 1;
            else
                left = mid + 1;
        }

        return left;
    }
    public boolean canGet(int min, int[][] grid, boolean [][] visited, int i, int j){
        if(i == rows - 1 && j == rows - 1)
            return true;
        visited[i][j] = true;
        for(int[] curDirect : directions){
            int newRow = i + curDirect[0];
            int newColumns = j + curDirect[1];
            if(newRow >= 0 && newColumns >= 0 && newRow < rows && newColumns < rows && !visited[newRow][newColumns] && Math.max(grid[newRow][newColumns], grid[i][j]) <= min)
            {
                if(canGet(min, grid, visited, newRow, newColumns))
                    return true;
            }
        }

        return false;
    }
}
复制代码

猜你喜欢

转载自juejin.im/post/7032676375890755597