【华为OD题库-079】周末爬山-Java

题目

周末小明准备去爬山锻炼,0代表平地,山的高度使用1到9来表示,小明每次爬山或下山高度只能相差k及k以内,每次只能上下左右一个方向上移动—格,小明从左上角(0,0)位置出发
输入描述
第一行输入m n k(空格分隔)。代表m*n的二维山地图,k为小明每次爬山或下山高度差的最大值
然后接下来输入山地图,一共m行n列,均以空格分隔。
取值范围:
0<m <= 500
0<<=5000<k<5
输出描述
请问小明能爬到的最高峰多高,到该最高峰的最短步数,输出以空格分隔。同高度的山峰输出较短步数.,如果没有可以爬的山峰,则高度和步数都返回0
示例1:
输入
5 4 1
0 1 2 0
1 0 0 0
1 0 1 2
1 3 1 0
0 0 0 9
输出
2 2
说明
根据山地图可知,能爬到的最高峰在(0,2)位置,高度为2,最短路径为(0.0)-(0,1)-(0,2),最短步数为2。
示例2:
输入
5 4 3
0 0 0 0
0 0 0 0
0 9 0 0
0 0 0 0
0 0 0 9
输出说明
0 0
根据山地图可知,每次爬山距离3,无法爬到山峰上,步数为0.

思路

【华为OD题库-001】宜居星球改造计划

只是在之前的基础上加了一个差值不超过k的限制

题解

package hwod;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;

public class ClimbingMountains {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[][] nums = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                nums[i][j] = sc.nextInt();
            }
        }
        int[] res = climbingMountains(nums, k);
        System.out.println(res[0] + " " + res[1]);
    }

    private static int[] x_axis = new int[]{1, 0, -1, 0};
    private static int[] y_axis = new int[]{0, 1, 0, -1};
    

    private static int[] climbingMountains(int[][] nums, int k) {
        int m = nums.length, n = nums[0].length;
        int[] used = new int[m * n];
        LinkedList<Integer> queue = new LinkedList<>();
        queue.addLast(0);
        used[0] = 1;
        int maxHeight = nums[0][0], minStep = 0;
        Map<Integer, Integer> map = new HashMap<>();
        while (!queue.isEmpty()) {
            int cur = queue.removeFirst();
            int x = cur / n, y = cur % n;
            for (int i = 0; i < 4; i++) {
                int new_x = x + x_axis[i], new_y = y + y_axis[i];
                int newIdx = new_x * n + new_y;//下一个坐标的位置
                if (new_x >= 0 && new_x < nums.length && new_y >= 0 && new_y < nums[0].length
                        && used[newIdx] == 0
                        && Math.abs(nums[new_x][new_y] - nums[x][y]) <= k) {
                    used[newIdx] = 1;
                    queue.addLast(newIdx);
                    map.put(newIdx, map.getOrDefault(cur, 0) + 1);
                    if (nums[new_x][new_y] >= maxHeight) {
                        if (nums[new_x][new_y] == maxHeight) {
                            minStep = Math.min(map.getOrDefault(newIdx, 0), minStep);
                        } else {
                            minStep = map.getOrDefault(newIdx, 0);
                        }
                        maxHeight = nums[new_x][new_y];
                    }

                }

            }

        }
        return new int[]{maxHeight, minStep};
    }
}

推荐

如果你对本系列的其他题目感兴趣,可以参考华为OD机试真题及题解(JAVA),查看当前专栏更新的所有题目。

说明

本专栏所有文章均为原创,欢迎转载,请注明文章出处:https://blog.csdn.net/qq_31076523/article/details/134176793。百度和各类采集站皆不可信,搜索请谨慎鉴别。技术类文章一般都有时效性,本人习惯不定期对自己的博文进行修正和更新,因此请访问出处以查看本文的最新版本。

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值