华为OD机考E卷200分题 - 智能驾驶

最新华为OD机试

题目描述

有一辆汽车需要从 m * n 的地图左上角(起点)开往地图的右下角(终点),去往每一个地区都需要消耗一定的油量,加油站可进行加油。

请你计算汽车确保从从起点到达终点时所需的最少初始油量。

说明:

  1. 智能汽车可以上下左右四个方向移动
  2. 地图上的数字取值是 0 或 -1 或 正整数:
    • -1 :表示加油站,可以加满油,汽车的油箱容量最大为100;
    • 0 :表示这个地区是障碍物,汽车不能通过
    • 正整数:表示汽车走过这个地区的耗油量
  3. 如果汽车无论如何都无法到达终点,则返回 -1

输入描述

第一行为两个数字,M,N,表示地图的大小为 M * N

  • 0 < M,N ≤ 200

后面一个 M * N 的矩阵,其中的值是 0 或 -1 或正整数,加油站的总数不超过 200 个

输出描述

如果汽车无论如何都无法到达终点,则返回 -1

如果汽车可以到达终点,则返回最少的初始油量

示例1

输入

2,2
10,20
30,40
123

输出

70
1

说明

行走的路线为:右→下

示例2

输入

4,4
10,30,30,20
30,30,-1,10
0,20,20,40
10,-1,30,40
12345

输出

70
1

说明

行走的路线为:右→右→下→下→下→右

示例3

输入

4,5
10,0,30,-1,10
30,0,20,0,20
10,0,10,0,30
10,-1,30,0,10
12345

输出

60
1

说明

行走的路线为:下→下→下→右→右→上→上→上→右→右→下→下→下

示例4

输入

4,4
10,30,30,20
30,30,20,10
10,20,10,40
10,20,30,40
12345

输出

-1
1

说明

无论如何都无法到达终点

解题思路

Java

import java.util.*;

public class Main {
   
   
    // 定义常量,表示汽车油箱的最大容量
    private static final int MAX_FUEL = 100; 

    public static void main(String[] args) {
   
   
        Scanner scanner = new Scanner(System.in).useDelimiter(",|\\s+");
        // 读取地图的行数和列数
        int numRows = scanner.nextInt();
        int numCols = scanner.nextInt();
        // 创建地图数组
        int[][] map = new int[numRows][numCols];

        // 填充地图数组,读取每个单元的值
        for (int i = 0; i < numRows; i++) {
   
   
            for (int j = 0; j < numCols; j++) {
   
   
                map[i][j] = scanner.nextInt();
            }
        }

        // 计算并获取最小初始油量
        int minFuel = findMinimumInitialFuel(map, numRows, numCols);
        // 输出计算结果
        System.out.println(minFuel);
    }

    // 使用二分查找方法确定从起点到终点所需的最小初始油量
    private static int findMinimumInitialFuel(int[][] map, int numRows, int numCols) {
   
   
        int low = 0, high = MAX_FUEL, optimalFuel = -1; // 初始化二分查找的上下界及结果

        while (low <= high) {
   
   
            int mid = (low + high) / 2;
            // 判断当前中值油量是否足以到达终点
            if (canReachDestination(map, mid, numRows, numCols)) {
   
   
                optimalFuel = mid; // 更新找到的可行油量
                high = mid - 1; // 尝试寻找更小的可行油量
            } else {
   
   
                low = mid + 1; // 增加油量尝试
            }
        }
        return optimalFuel; // 返回最小初始油量,如果无法到达则返回-1
    }

    // 检查给定起始油量是否足以从起点到达终点
    private static boolean canReachDestination(int[][] map, int startFuel, int numRows, int numCols) {
   
   
        if (map[0][0] == 0) return false; // 起点如果是障碍物,则无法出发

        // 初始化每个位置的剩余油量数组
        int[][] remainingFuel = new int[numRows][numCols];
        for (int[] row : remainingFuel) {
   
   
            Arrays.fill(row, -1);
        }
        // 设置起点的初始油量
        remainingFuel[0][0] = map[0][0] == -1 ? MAX_FUEL : startFuel - map[0][0];
        if (remainingFuel[0][0] < 0) return false; // 如果起始油量不足以离开起点,返回false

        // 使用优先队列按照油量从大到小进行搜索
        PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> b[2] - a[2]);
        priorityQueue.offer(new int[]{
   
   0, 0, remainingFuel[0][0]});
        int[] dx = {
   
   0, 1, 0, -1}; // 搜索方向数组x
        int[] dy = {
   
   1, 0, -1, 0}; // 搜索方向数组y

        // BFS搜索
        while (!priorityQueue.isEmpty()) {
   
   
            int[] current = priorityQueue.poll();
            int currentRow = current[0], currentCol = current[1], fuel = current[2];

            if (currentRow == numRows - 1 && currentCol == numCols - 1) return true; // 如果到达终点,则返回true

            // 检查四个方向
            for (int direction = 0; direction < 4; direction++) {
   
   
                int newRow = currentRow + dx[direction], newCol = currentCol + dy[direction];
                if (isValid(newRow, newCol, numRows, numCols, map)) {
   
    // 检查新位置是否有效
                    int newFuel = map[newRow][newCol] == -1 ? MAX_FUEL : fuel - map[newRow][newCol];
                    if (newFuel > remainingFuel[newRow][newCol]) {
   
   
                        remainingFuel[newRow][newCol] = newFuel;
                        priorityQueue.offer(new int[
### 关于华为OD机考中的智能驾驶目及备考资料 对于想要参加华为OD机考并专注于智能驾驶方向的考生来说,准备过程需要特别关注该领域内的算法实现以及实际应用场景的理解。根据以往的经验,在这类考试中可能会遇到涉及路径规划、目标检测与识别等方面的知识点[^1]。 #### 路径规划类目 此类问通常会给出地图数据结构作为输入参数,并要求编写程序来计算最优行驶路线。这可能涉及到A*搜索算法的应用实例: ```python from heapq import heappop, heappush def a_star_search(graph, start, goal): frontier = [] heappush(frontier, (0, start)) came_from = {} cost_so_far = {start: 0} while frontier: _, current = heappop(frontier) if current == goal: break for next_node in graph.neighbors(current): new_cost = cost_so_far[current] + graph.cost(current, next_node) if next_node not in cost_so_far or new_cost < cost_so_far[next_node]: cost_so_far[next_node] = new_cost priority = new_cost + heuristic(goal, next_node) heappush(frontier, (priority, next_node)) came_from[next_node] = current return reconstruct_path(came_from, start=start, goal=goal) def heuristic(a, b): # 使用欧几里得距离或其他合适的启发函数 pass def reconstruct_path(came_from, start=None, goal=None): path = [goal] while True: try: last_step = came_from[path[-1]] path.append(last_step) if last_step == start: break except KeyError as e: print('No valid path found') return None return list(reversed(path)) ``` 此代码片段展示了如何利用优先队列实现带权重图上的最短路径查找功能,适用于模拟车辆导航场景下的路径优化任务。 #### 目标检测与识别类目 这部内容往往围绕着计算机视觉技术展开讨论,特别是基于深度学习框架的目标类模型训练方法及其应用实践。例如YOLOv3网络架构可以用于快速而准确地标记图像内出现的对象类别信息: ```python import torch from torchvision.models.detection import fasterrcnn_resnet50_fpn model = fasterrcnn_resnet50_fpn(pretrained=True).eval() # 假设img是一个PIL.Image对象表示待处理图片 output = model([torch.tensor(img).permute(2, 0, 1)]) for box, label, score in zip(*[iter(output['boxes']), iter(output['labels']), iter(output['scores'])]): if score > threshold: draw_bounding_box_on_image(image=img, bbox=box.tolist(), class_name=COCO_INSTANCE_CATEGORY_NAMES[label]) ``` 上述Python脚本说明了怎样加载预训练好的Faster R-CNN ResNet-50 FPN模型来进行实时物体探测操作,这对于理解自动驾驶汽车是如何感知周围环境具有重要意义。 为了更好地应对这些挑战性的编程测试环节,建议参考《橡皮擦》系列教程以及其他公开资源深入研究相关主;同时也要注重练习历年真以熟悉命风格和难度水平。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值