当前位置:网站首页>Leecode learning notes - the shortest path for a robot to reach its destination
Leecode learning notes - the shortest path for a robot to reach its destination
2022-06-25 02:35:00 【Engineering students who like history】
link :https://www.nowcoder.com/questionTerminal/a4d87cae999244acbdd57bb0385a1d0b
source : Cattle from
Small C Recently, I am studying robots , He wanted to see if his robot was smart enough , So he put the robot in a n*m In the maze of , See if the robot can reach its destination in the shortest time , But small C I don't know the shortest time , Now please help him calculate the shortest time for the robot to reach its destination ?
Enter two integers in the first row of data n and m.n and m The scope of the [10,500]. Next n That's ok , Each row m Elements , Each square of the maze . 'S’ Represents the starting point of the robot ,
'T’ Means the destination , '#' Indicates that the square cannot pass , '.' Means that it can be passed .
An integer is output to indicate the shortest time for the robot to reach the destination , If the robot can't reach its destination , Output -1.
Input
3 3
S…
##.
.T.
Output
5
First of all, this question uses dfs Overtime
Why do I need to use bfs?bfs Be similar to “ Ripple of water ” Spread out , That is, the first point that meets the conditions is the nearest point . The difficulty here is to record the distance from each point to the starting point , That is, in addition to the location information in the queue , Also put distance information , Every point should have , Why? ? Here's the picture , In fact, both sides can walk , But the left side comes first and returns , Therefore, in addition to recording the information of each point, the distance information should also be recorded 
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});
}
}
}
}
边栏推荐
- leecode学习笔记-机器人走到终点的最短路径
- [live review] battle code pioneer phase 7: how third-party application developers contribute to open source
- Android Internet of things application development (smart Park) - set sensor threshold dialog interface
- LINQ query (3)
- 左手梦想 右手责任 广汽本田不光关注销量 还有儿童安全
- UnityShader入门精要——表面着色器
- Exploring the mystery of C language program -- C language program compilation and preprocessing
- jwt
- When an interface has an exception, how do you analyze the exception?
- ida中交叉引用的解析
猜你喜欢

进入阿里做测试员遥不可及?这里或许有你想要的答案

Can automate - 10k, can automate - 20K, do you understand automated testing?

Unity存档系统——Json格式的文件

当他们在私域里,掌握了分寸感

转行软件测试2年了,给还在犹豫的女生一点建议

会自动化—10K,能做自动化—20K,你搞懂自动化测试没有?

qt打包exe文件,解决“无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”

Pit entry machine learning: I. Introduction

都2022年了,你还不了解什么是性能测试?

记一次beego通过go get命令后找不到bee.exe的坑
随机推荐
保险APP适老化服务评测分析2022第06期
It's 2022, and you still don't know what performance testing is?
探索C语言程序奥秘——C语言程序编译与预处理
Network planning | [four network layers] knowledge points and examples
李宏毅《机器学习》丨6. Convolutional Neural Network(卷积神经网络)
指南针靠谱吗?开证券账户安全吗?
E - average and median
1-6搭建Win7虚拟机环境
当他们在私域里,掌握了分寸感
入坑机器学习:一,绪论
Call system function security scheme
yarn : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本
Detailed explanation of cache (for the postgraduate entrance examination of XD)
How to get the picture outside the chain - Netease photo album [easy to understand]
Summary of stack frame in arm assembly
Once beego failed to find bee after passing the go get command Exe's pit
Planification du réseau | [quatre couches de réseau] points de connaissance et exemples
What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?
Kaggle 专利匹配比赛金牌方案赛后总结
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro