[华为OD]C卷 BFS 亲子游戏 200

embedded/2024/9/23 14:36:39/

题目:

宝宝和妈妈参加亲子游戏,在一个二维矩阵(N*N)的格子地图上,宝宝和妈妈抽签决定各自 

的位置,地图上每个格子有不同的Q糖果数量,部分格子有障碍物。

游戏规则Q是妈妈必须在最短的时间(每个单位时间只能走一步)到达宝宝的位置,路上的

所有糖果都可以拿走,不能走障碍物的格子,只能上下左右走。

请问妈妈在最短到达宝宝位置的时间内最多拿到多少糖果(优先考虑最短时间到达的情况下尽 

可能多拿糖果)。

输入描述

第一行输入为N, N标识二维矩阵的大小

之后N行,每行有N个值,表格矩阵每个位置的值

其中:

- 3:妈妈

- 2:宝宝

- 1:障碍

> =0:糖果数(0表示没有糖果,但是可以走)

输出描述

输出妈妈在最短到达宝宝位置的时间内最多拿到多少糖果,行末无多余空格

备注

地图最大50*50

示例1:

输入

4

3 2 1 -3

1 -1 1 1

1 1 -1 2

-2 1 2 3

输出

9

说明

此地图有两条最短路径Q可到宝宝位置, 都是最短路径6步,但先向下再向左可以拿到9个糖

示例2:

输入

4

3 2 1 -3

-1 -1 1 1

1 1 -1 2

-2 1 -1 3

输出

-1

说明

此地图妈妈无法到达宝宝位置

题解:

图求最短路径,采用BFS搜索,关于BFS,推荐观看:BFS广搜解决迷宫问题_哔哩哔哩_bilibili

看完基本就清楚BFS算法原理了。

这题里面因为有糖果数目,所以这边采用的方法是,如果下一步能走到终点,那么终点位置的visit[endx][endy]不设置为1.这样其他方案走到终点的话,也能加入进队列里面。但是由于队列里面取首元素是终点的话,那么就不会往下找了,所以,搜索记录的应该也都是比较短的路线。然后再依旧采用一个List记录到终点的步数,一个Map记录到终点的步数和糖果数的结果。最后找到最少的步数,对应的最多糖果数就可以了。

代码:

import java.util.*;public class FindCandy {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = Integer.valueOf(sc.nextLine());int nums[][] = new int[n][n];int stratPosX = Integer.MIN_VALUE;int stratPosY = Integer.MIN_VALUE;int childPosX = Integer.MIN_VALUE;int childPosY = Integer.MIN_VALUE;int visited[][] = new int[n][n];for (int i = 0; i < n; i++) {String path[] = sc.nextLine().split(" ");for (int j = 0; j < n; j++) {visited[i][j] = 0;nums[i][j] = Integer.valueOf(path[j]);if (nums[i][j] == -3) {stratPosX = i;stratPosY = j;visited[i][j] = 1;}if (nums[i][j] == -2) {childPosX = i;childPosY = j;}}}int[][] directions = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};Queue<Steps> queue = new LinkedList<>();Steps firstStep = new Steps(stratPosX, stratPosY, 0, 0);visited[stratPosX][stratPosY] = 1;Steps endStep = new Steps(childPosX, childPosY, 0, 0);queue.offer(firstStep);List<Integer> finalRoteSteps = new ArrayList<>();Map<Integer, List<Integer>> stepCandyMap = new HashMap<>();boolean hasRoote = false;while (!queue.isEmpty()) {Steps frontStep = ((LinkedList<Steps>) queue).getFirst();if (frontStep.x == endStep.x && firstStep.y == endStep.y) {hasRoote = true;continue;}for (int i = 0; i < 4; i++) {int newX = frontStep.x + directions[i][0];int newY = frontStep.y + directions[i][1];if (newX >= 0 && newX < n && newY >= 0 && newY < n && nums[newX][newY] != -1 && visited[newX][newY] == 0) {
//                    System.out.println("newX= " + newX + " newY " + newY + " candy " + nums[newX][newY]);Steps nextStep = new Steps();if (newX == endStep.x && newY == endStep.y) {nextStep = new Steps(newX, newY, frontStep.getStep() + 1, frontStep.getCandy());queue.offer(nextStep);finalRoteSteps.add(nextStep.step);List<Integer> candyList = stepCandyMap.containsKey(nextStep.step) ? stepCandyMap.get(nextStep.step) :new ArrayList<>();if (!candyList.contains(nextStep.candy)) {candyList.add(nextStep.candy);}stepCandyMap.put(nextStep.step, candyList);hasRoote = true;} else {nextStep = new Steps(newX, newY, frontStep.getStep() + 1, frontStep.getCandy() + nums[newX][newY]);visited[newX][newY] = 1;queue.offer(nextStep);}}}((LinkedList<Steps>) queue).pollFirst();}if (hasRoote) {Collections.sort(finalRoteSteps);List<Integer> candys = stepCandyMap.get(finalRoteSteps.get(0));Collections.sort(candys);System.out.println(candys.get(candys.size() - 1));} else {System.out.println(-1);}}
}class Steps {int x;int y;int step;int candy;public Steps() {}public Steps(int x, int y, int step, int candy) {this.x = x;this.y = y;this.step = step;this.candy = candy;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getStep() {return step;}public void setStep(int step) {this.step = step;}public int getCandy() {return candy;}public void setCandy(int candy) {this.candy = candy;}
}

验证:


http://www.ppmy.cn/embedded/38403.html

相关文章

Apache 开源项目文档中心 (英文 + 中文)

进度&#xff1a;持续更新中。。。 Apache Ambari 2.7.5 Apache Ambari Installation 2.7.5.0 (latest)Apache Ambari Installation 2.7.5.0 中文版 (latest)

Linux-笔记 集成更新PDA

1、更换PDA文件 将pda包放到ubuntu中解压后将里面的opt文件夹和usr文件夹覆盖到以下路径&#xff1a; /buildroot-201611/target/user_rootfs_extra 2、修改启动脚本文件 进入路径 /buildroot-201611/target/user_rootfs_extra/etc/init.d&#xff0c;修改该路径下的rc_daemon…

程序设计:C++11原子 写优先的读写锁(源码详解)

初级代码游戏的专栏介绍与文章目录-CSDN博客 我的github&#xff1a;codetoys&#xff0c;所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。 这些代码大部分以Linux为目标但部分代码是纯C的&#xff0c;可以在任何平台上使用。 github位置&#xff1a;codeto…

docker————docker的安装

目录 docker的安装 1、安装yum-utils工具 2、安装yum仓库 3、安装docker引擎 4、设置开机启动&#xff0c;并立即启动 5、测试 docker的安装 docker的官网Docker Docs 我才用的linux版本是rocky&#xff0c;使用的是最小安装 1、安装yum-utils工具 [rootbogon yum.rep…

【八十二】【算法分析与设计】2421. 好路径的数目,928. 尽量减少恶意软件的传播 II,并查集的应用,元素信息绑定下标一起排序,元素通过下标进行绑定

2421. 好路径的数目 给你一棵 n 个节点的树&#xff08;连通无向无环的图&#xff09;&#xff0c;节点编号从 0 到 n - 1 且恰好有 n - 1 条边。 给你一个长度为 n 下标从 0 开始的整数数组 vals &#xff0c;分别表示每个节点的值。同时给你一个二维整数数组 edges &#xff…

Django Admin后台管理:高效开发与实践

title: Django Admin后台管理&#xff1a;高效开发与实践 date: 2024/5/8 14:24:15 updated: 2024/5/8 14:24:15 categories: 后端开发 tags: DjangoAdmin模型管理用户认证数据优化自定义扩展实战案例性能安全 第1章&#xff1a;Django Admin基础 1.1 Django Admin简介 Dj…

【excel】数据非数值导致排序失效

场景 存在待排序列的数值列&#xff0c;但排序失效&#xff0c;提示类型有问题&#xff1a; 解决 选中该列&#xff0c;数据→分列 而后发现提示消失&#xff0c;识别为数字&#xff0c;可正常排序。

VBA助力Excel工作薄另存备份和自我销毁,确保工作薄名称自始至终不被修改

VBA助力Excel工作薄另存备份和自我销毁,确保工作薄名称自始至终不被修改 工作中,你是否遇到过这样的情况,工作表改来改去,工作薄名称也从“初稿”、“终稿”、“最终版”、“上交版”等逐步变化,最后也不知道哪版是最后一版,想从修改日期查看哪个是最后一版,可逐个打开关…