最小体力消耗路径(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
*/
public void union(int index1,int index2){
int root1=find(index1);
int root2=find(index2);
if(root1==root2){return;}
if(rank[root1]>rank[root2]){
parent[root2]=root1;
}else if(rank[root1]<rank[root2]){
parent[root1]=root2;
}else{
parent[root2]=root1;
rank[root1]++;
}
}
}
3. 迪杰斯特拉算法(官方题解)
package com.lxf.dijstra;
import java.util.Arrays;
import java.util.PriorityQueue;
public class MinimumEffortPath {
//方向计算数组:上右下左
int[][] dirs=new int[][]{{-1,0},{0,1},{1,0},{0,-1}};
public int minimumEffortPath(int[][] heights) {
int row=heights.length;//heights数组行
int col=heights[0].length;//heights数组列
int length=row*col;//heights数组用一维数组存储的长度
//存储从(0,0)顶点到当前顶点所需要花费的最小体力值数组
int[] dists = new int[length];
//设置数组初始值为int的最大值
Arrays.fill(dists,Integer.MAX_VALUE);
//设置起点最小体力值为0
dists[0]=0;
//记录顶点是否访问过的数组
boolean[] visited = new boolean[length];
//存储边的优先队列
PriorityQueue<int[]> pq = new PriorityQueue<>((arr1, arr2) -> arr1[2] - arr2[2]);
pq.offer(new int[]{0,0,0});//添加起始数组
while (!pq.isEmpty()) {
int[] poll = pq.poll();
int x = poll[0], y = poll[1], d = poll[2];
//当前顶点的一维坐标
int index=x*col+y;
if(visited[index]){
//如果当前顶点已经访问过,直接取下一个顶点
continue;
}
visited[index]=true;//标记当前下标已被访问
if(index==length-1){//如果到达了最后顶点,直接跳出循环
break;
}
for (int i = 0; i < dirs.length; i++) {
int nx=x+dirs[i][0];
int ny=y+dirs[i][1];
if(nx>=0&&ny>=0&&nx<row&&ny<col&&Math.max(d,Math.abs(heights[nx][ny]-heights[x][y]))<dists[nx*col+ny]){
//当向上右下左走一个顶点,并且走完这个顶点花费的体力值要比其它路径花费的小
//更新结点的体力值,并将结点加入队列
dists[nx*col+ny]=Math.max(d,Math.abs(heights[nx][ny]-heights[x][y]));
pq.offer(new int[]{nx,ny,dists[nx*col+ny]});
}
}
}
return dists[length-1];
}
}