当前位置 博文首页 > Inmaturity_7的博客:算法练习帖--66--最小体力消耗路径(Java)

    Inmaturity_7的博客:算法练习帖--66--最小体力消耗路径(Java)

    作者:[db:作者] 时间:2021-07-16 21:48

    最小体力消耗路径(Java)

    一、题目简介

    你准备参加一场远足活动。给你一个二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 (row, col) 的高度。一开始你在最左上角的格子 (0, 0) ,且你希望去最右下角的格子 (rows-1, columns-1) (注意下标从 0 开始编号)。你每次可以往 上,下,左,右 四个方向之一移动,你想要找到耗费 体力 最小的一条路径。

    一条路径耗费的 体力值 是路径上相邻格子之间 高度差绝对值 的 最大值 决定的。

    请你返回从左上角走到右下角的最小 体力消耗值 。
    (题目来源:力扣(LeetCode))
    示例 1:
    在这里插入图片描述

    输入:heights = [[1,2,2],[3,8,2],[5,3,5]]
    输出:2
    解释:路径 [1,3,5,3,5] 连续格子的差值绝对值最大为 2 。
    这条路径比路径 [1,2,2,2,5] 更优,因为另一条路径差值最大值为 3 。
    

    示例 2:
    在这里插入图片描述

    输入:heights = [[1,2,3],[3,8,4],[5,3,5]]
    输出:1
    解释:路径 [1,2,3,4,5] 的相邻格子差值绝对值最大为 1 ,比路径 [1,3,5,3,5] 更优。
    

    示例 3:
    在这里插入图片描述

    输入:heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]
    输出:0
    解释:上图所示路径不需要消耗任何体力。
    
    提示:
    rows == heights.length
    columns == heights[i].length
    1 <= rows, columns <= 100
    1 <= heights[i][j] <= 106
    

    二、解决方法

    1. 二分+bfs(官方题解)

    package com.lxf.test3;
    
    import java.util.LinkedList;
    import java.util.Queue;
    
    class Solution {
        int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
        public int minimumEffortPath(int[][] heights) {
            int m = heights.length;//行数
            int n = heights[0].length;//列数
            //因为1=<heights[i][j]<=1000000,所以体力消耗值最小值在0-999999中
            //所以我们在这区间二分查找就可以了
            int left = 0, right = 999999, ans = 0;
            while (left <= right) {//当left==right时,left和right和mid都是最小体力消耗值
                int mid = (left + right) / 2;//取中间值
                Queue<int[]> queue = new LinkedList<int[]>();
                //加入初始点
                queue.offer(new int[]{0, 0});
                //记录当前顶点是否被访问
                boolean[] seen = new boolean[m * n];
                seen[0] = true;
                //bfs遍历整张图,把体力消耗值<=mid的全部走一边
                while (!queue.isEmpty()) {
                    int[] cell = queue.poll();//取出当前点坐标
                    int x = cell[0], y = cell[1];
                    for (int i = 0; i < 4; ++i) {
                        //将当前顶点的上下左右顶点遍历
                        int nx = x + dirs[i][0];
                        int ny = y + dirs[i][1];
                        //如果上下左右顶点没越界,并且体力消耗值<=mid
                        //我们就走这个点,并标志为已访问过
                        if (nx >= 0 && nx < m && ny >= 0 && ny < n && !seen[nx * n + ny] && Math.abs(heights[x][y] - heights[nx][ny]) <= mid) {
                            queue.offer(new int[]{nx, ny});
                            seen[nx * n + ny] = true;
                        }
                    }
                }
                //遍历完图之后判断最后一个顶点访问过没有
                if (seen[m * n - 1]) {
                    //访问到了,说明最小体力消耗值在当前区间mid中点的左区间
                    ans = mid;
                    right = mid - 1;
                } else {
                    //没有访问到了,说明最小体力消耗值在当前区间mid中点的右区间
                    left = mid + 1;
                }
            }
            return ans;
        }
    }
    
    

    2. 并查集+克鲁斯卡尔算法(参照官方题解)

    package com.lxf.bfs;
    
    
    import java.util.PriorityQueue;
    
    public class MinimumEffortPath {
        public static void main(String[] args) {
            MinimumEffortPath m = new MinimumEffortPath();
            m.minimumEffortPath(new int[][]{{1,2,2},{3,8,2},{5,3,5}});
        }
        int[] parent;
        int[] rank;
        public int minimumEffortPath(int[][] heights) {
            int row=heights.length;
            int col=heights[0].length;
            parent=new int[row*col];//父结点数组
            rank=new int[row*col];//秩结点数组
            //数组存储两个结点和两结点的权值,并用小根堆存储
            PriorityQueue<int[]> pq = new PriorityQueue<>((arr1, arr2) -> arr1[2] - arr2[2]);
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    //二维数组一维化
                    int index=i*col+j;
                    parent[index]=index;//父结点数组初始化
                    //将当前结点的上和左结点移动到当前结点的体力值记录到数组中,并存储到小根堆
                    if(i>0){
                        pq.offer(new int[]{index-col,index,Math.abs(heights[i][j]-heights[i-1][j])});
                    }
                    if(j>0){
                        pq.offer(new int[]{index-1,index,Math.abs(heights[i][j]-heights[i][j-1])});
                    }
                }
            }
            int min=0;//最小体力值
            while(!pq.isEmpty()){
                //依次取出小根堆中的值,也就是已经从体力值最小一直取
                int[] nums = pq.poll();
                //合并结点
                union(nums[0],nums[1]);
                if(find(0)==find(row*col-1)){
                    //如果开始结点和最后结点相通,说明已经连通了,当前体力值就是最小花费体力值
                    min=nums[2];
                    break;
                }
            }
            return min;
        }
    
        /**
         * 查找父结点方法
         * @param index
         * @return
         */
        public int find(int index){
            while(index!=parent[index]){
                parent[index]=parent[parent[index]];
                index=parent[index];
            }
            return index;
        }
    
        /**
         * 合并集合方法
         * @param index1
         * @param index2
         */