当前位置: 首页 > news >正文

华为OD机试真题【上班之路】

1、题目描述

【上班之路】
Jungle 生活在美丽的蓝鲸城,大马路都是方方正正,但是每天马路的封闭情况都不一样。
地图由以下元素组成:
1)”.” — 空地,可以达到;
2)”*” — 路障,不可达到;
3)”S” — Jungle的家;
4)”T” — 公司.
其中我们会限制Jungle拐弯的次数,同时Jungle可以清除给定个数的路障,现在你的任务是计算Jungle是否可以从家里出发到达公司。

【输入描述】
输入的第一行为两个整数t,c(0<=t,c<=100), t代表可以拐弯的次数,c代表可以清除的路障个数。
输入的第二行为两个整数n,m(1<=n,m<=100),代表地图的大小。
接下来是n行包含m个字符的地图。n和m可能不一样大。
我们保证地图里有S和T。

【输出描述】
输出是否可以从家里出发到达公司,是则输出YES,不能则输出NO。

【示例1】
输入

2 0
5 5
..S..
****.
T....
****.
.....

输出
YES

【示例2】
输入:

1 2
5 5
.*S*.
*****
..*..
*****
T....

输出: NO
说明:该用例中,至少需要拐弯1次,清除3个路障,所以无法到达

2、解题思路

首先找到S的位置,再利用回溯算法从S位置开始遍历上、下、左、右四个方向可到达的位置,当到达公司位置T则代表可以到达公司,所有方向都遍历完成都无法到达T则无法到达公司。

3、参考代码

方法一:

import java.util.Scanner;/*** @Author Long* @Date 2023/5/3 20:45*/
public class 上班之路 {private static final int[][] directions = {{0, 1, 1}, {0, -1, 2}, {1, 0, 3}, {-1, 0, 4}};private static int maxTurns, maxClears, rows, cols;private static String[][] matrix;public static void main(String[] args) {Scanner in = new Scanner(System.in);while (in.hasNext()) {// 处理输入maxTurns = in.nextInt();maxClears = in.nextInt();rows = in.nextInt();cols = in.nextInt();matrix = new String[rows][cols];char[][] chars = new char[rows][cols];for (int i = 0; i < rows; i++) {String string = in.next();matrix[i] = string.split("");chars[i] = string.toCharArray();}for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {boolean[][] visited = new boolean[cols][rows];if ("S".equals(matrix[i][j])) {if (dfs(visited, i, j, 0, 0, 0)) {System.out.println("YES");return;} else {System.out.println("NO");return;}}}}System.out.println("NO");return;}}public static boolean dfs(boolean[][] visited, int x, int y, int turnsUsed, int clearsUsed, int lastDirection) {if ("T".equals(matrix[x][y])) {return true;}visited[x][y] = true;for (int[] direction : directions) {int curDirection = direction[2];int newX = x + direction[0];int newY = y + direction[1];boolean turnFlag = false;boolean breakFlag = false;if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && !visited[newX][newY]) {if (lastDirection != 0 && lastDirection != curDirection) {if (turnsUsed + 1 > maxTurns) {continue;}turnFlag = true;}if ("*".equals(matrix[newX][newY])) {if (clearsUsed + 1 > maxClears) {continue;}breakFlag = true;}if (dfs(visited, newX, newY, turnsUsed + (turnFlag ? 1: 0), clearsUsed + (breakFlag ? 1 : 0), curDirection)) {return true;}}}return false;}
}

方法二:

import java.util.Scanner;/*** @Author * @Date 2023/5/3 20:45*/
public class 上班之路 {private static final int[][] directions = {{0, 1, 1}, {0, -1, 2}, {1, 0, 3}, {-1, 0, 4}};public static void main(String[] args) {Scanner in = new Scanner(System.in);while (in.hasNext()) {// 处理输入int t = in.nextInt();int c = in.nextInt();int n = in.nextInt();int m = in.nextInt();char[][] chars = new char[n][m];for (int i = 0; i < n; i++) {String string = in.next();chars[i] = string.toCharArray();}int sX = 0;int sY = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (chars[i][j] == 'S') {sX = i;sY = j;break;}}if (chars[sX][sY] == 'S') {break;}}boolean[][] used = new boolean[n][m];if (chars[sX][sY] == 'S') {if (dfs(chars, sX, sY, t, c, 0, used)) {System.out.println("YES");} else {System.out.println("NO");}} else {System.out.println("NO");}}}public static boolean dfs(char[][] chars, int x, int y, int t, int c, int dis, boolean[][] used) {int n = chars.length;int m = chars[0].length;if (x < 0 || x >= n || y < 0 || y >= m) {return false;}if (chars[x][y] == 'T') {return true;}used[x][y] = true;for (int i = 0; i < directions.length; i++) {int newX = x + directions[i][0];int newY = y + directions[i][1];// 超过边界if (newX < 0 || newX >= n || newY < 0 || newY >= m || used[newX][newY]) {continue;}if (chars[newX][newY] == '*') {  // 当前是障碍if (c == 0) {  // 清除障碍数用完了continue;}if (dis == 0 || dis == directions[i][2]) {if (dfs(chars, newX, newY, t, c - 1, dis, used)) {return true;}} else {  // 上一步方向跟当前方向不一致if (t == 0) {  // 拐弯数用完continue;}if (dfs(chars, newX, newY, t - 1, c - 1, dis, used)) {return true;}}} else {if (dis == 0 || dis == directions[i][2]) {if (dfs(chars, newX, newY, t, c, dis, used)) {return true;}} else {if (t == 0) {continue;}if (dfs(chars, newX, newY, t - 1, c, dis, used)) {return true;}}}}used[x][y] = false;return false;}}

4、相似题目

(1)西天取经
(2)士兵突击

http://www.lryc.cn/news/111693.html

相关文章:

  • 【linux源码学习】【实验篇】使用bochs运行linux0.11系统(搭建一个自己的工作站)
  • java+springboot+mysql个人日记管理系统
  • 旋转图像 LeetCode热题100
  • Vue3 element-plus表单嵌套表格实现动态表单验证
  • VSCode插件Todo Tree的使用
  • 无人驾驶实战-第五课(动态环境感知与3D检测算法)
  • Tomcat 的内存配置
  • pycharm出现python test运行报错(pytest模式)
  • JavaScript篇 this指向
  • 操作系统复习总结1
  • Matlab中图的最短路径
  • 没有jodatime,rust里怎么将字符串转为日期呢?
  • 【Markdown入门及使用】
  • 大数据面试题:HBase的读写缓存
  • springboot基于vue的高校迎新系统的设计与实现8jf9e
  • JVM入门到精通
  • Hive执行引擎的区别
  • 分布式 - 服务器Nginx:常见问题总结(二)
  • 【Paper Reading】CenterNet:Keypoint Triplets for Object Detection
  • 【BASH】回顾与知识点梳理(三)
  • C#设计模式之---单例模式
  • Git工具安装
  • 深度学习——注意力机制、自注意力机制
  • STM32入门学习之定时器中断
  • 基本数据类型与包装数据类型的使用标准
  • 小研究 - 基于 SpringBoot 微服务架构下前后端分离的 MVVM 模型(二)
  • ArmSoM-W3之RK3588安装Qt+opencv+采集摄像头画面
  • 基于长短期神经网络的风速预测,基于LSTM的风速预测
  • Mybatis引出的一系列问题-spring多数据源配置
  • Vue-组件二次封装