当前位置:网站首页>leecode学习笔记-机器人走到终点的最短路径
leecode学习笔记-机器人走到终点的最短路径
2022-06-24 23:01:00 【喜欢历史的工科生】
链接:https://www.nowcoder.com/questionTerminal/a4d87cae999244acbdd57bb0385a1d0b
来源:牛客网
小C最近在研究机器人,他想看看自己的机器人够不够智能,于是他将机器人放在一个n*m的迷宫中,看看机器人能不能在最短的时间内到达目的地,可是小C不知道最短的时间是多少,现在请你帮他算算机器人到达目的地的最短时间是多少?
输入数据第一行两个整数n和m。n和m的范围[10,500]。 接下来n行,每行m个元素,表示迷宫的每个方格。 'S’表示机器人的出发点,
'T’表示目的地, '#'表示该方格不能通过, '.'表示可以通过。
输出一个整数表示机器人到达目的地的最短时间,如果机器人不能到达目的地,输出-1。
输入
3 3
S…
##.
.T.
输出
5
首先这个题用dfs超时
为什么需要用bfs?bfs类似于“水的波纹”向外扩散,那就是最先找到符合条件的点就是最近的点。这里的难点就是要记录每个点距离出发的距离,即队列中除了放位置信息外,还要放距离信息,每个点都要有,为什么呢?如下图,左右其实都会走,但是左侧先到就会返回了,所以除了记录每个点位信息还要记录距离信息
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main{
public static int path = 0;
public static int min = Integer.MAX_VALUE;
public static void main(String[] args) {
// int m = 3,n = 3;
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
char[][] input = new char[m][n];
sc.nextLine();
for (int i = 0; i < m; i++) {
String s = sc.nextLine();
for (int j = 0; j < n; j++) {
input[i][j] = s.charAt(j);
}
}
// for (int i = 0; i < m; i++) {
// for (int j = 0; j < n; j ++) {
// System.out.print(input[i][j] + " ");
// }
// System.out.println();
// }
boolean[][] visited = new boolean[input.length][input[0].length];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (input[i][j] == 'S') {
// visited[i][j] =true;
bfs(input, i, j, visited);
break;
}
}
}
if (min == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(min);
}
}
// public static void dfs(char[][] input, int i, int j) {
// if (i < 0 || i >= input.length || j < 0 || j >= input[0].length ||
// input[i][j] == '#') {
// return;
// }
//
// if (input[i][j] == 'T') {
// min = Math.min(min, path);
// return;
// }
// path++;
// input[i][j] = '#';
// dfs(input, i + 1, j);
// dfs(input, i - 1, j);
// dfs(input, i, j + 1);
// dfs(input, i, j - 1);
// input[i][j] = '.';
// path--;
// }
public static void bfs(char[][] input, int i, int j, boolean[][] visited) {
Queue<int[]> que = new LinkedList<>();
que.offer(new int[]{
i, j, 0});
while (!que.isEmpty()) {
int[] temp = que.poll();
i = temp[0];
j = temp[1];
if (i >= 0 && i < input.length && j >=0 && j < input[0].length && input[i][j] != '#' && visited[i][j] == false ) {
if (input[i][j] == 'T') {
// for (int k = 0; k < visited.length; k ++) {
// for (int h = 0 ; h < visited[0].length; h++) {
// System.out.print(visited[k][h] + " ");
// }
// System.out.println();
// }
min = temp[2];
return;
}
visited[i][j] = true;
que.offer(new int[]{
i,j + 1, temp[2] + 1});
que.offer(new int[]{
i,j - 1, temp[2] + 1});
que.offer(new int[]{
i + 1,j, temp[2] + 1});
que.offer(new int[]{
i - 1,j, temp[2] + 1});
}
}
}
}
边栏推荐
- Hashcat 的使用
- When an interface has an exception, how do you analyze the exception?
- Smartctl opens the device and encounters permission denied problem troubleshooting process record
- random list随机生成不重复数
- What are the SQL aggregate functions
- AI服装生成,帮你完成服装设计的最后一步
- [I.MX6UL] U-Boot移植(六) 网络驱动修改 LAN8720A
- MCN机构遍地开花:博主和作者要谨慎签约、行业水很深
- Summary of stack frame in arm assembly
- 02-Epicor二次开发常用代码
猜你喜欢

罗德与施瓦茨与中关村泛联院合作开展6G技术研究与早期验证

内网学习笔记(7)
Cusdis - lightweight, privacy first open source comment system | chain of the city

QT package the EXE file to solve the problem that "the program input point \u zdapvj cannot be located in the dynamic link library qt5cored.dll"

yarn : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本

The ecosystem of the yuan universe

一线城市软件测试工资——你拖后腿了吗

Is it out of reach to enter Ali as a tester? Here may be the answer you want

记一次beego通过go get命令后找不到bee.exe的坑
Cusdis - 轻量级、隐私优先的开源评论系统 | 倾城之链
随机推荐
同花顺是正规平台吗?同花顺开户安全吗
Beescms website penetration test and repair comments "suggestions collection"
Convert string array to list collection
Basic layout -qhboxlayout class, qvboxlayout class, qgridlayout class
【Proteus仿真】Arduino UNO+继电器控制照明设备
How can Huatai Securities open an account to achieve one in ten thousand? Are securities accounts safe and reliable
ARM汇编中的栈桢小结
Exploring the mystery of C language program -- C language program compilation and preprocessing
Use of hashcat
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
02 common codes for Epicor secondary development
如何卸载cuda
疫情防控,居家办公,网上授课之心得 | 社区征文
Intranet learning notes (7)
Talking about the advantages of flying book in development work | community essay solicitation
MOS tube related knowledge
Sumati gamefi ecological overview, element design in the magical world
The Oracle 11g RAC cluster database cannot be started due to directory permission errors
AI服装生成,帮你完成服装设计的最后一步
What are the SQL aggregate functions