当前位置:网站首页>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});
}
}
}
}
边栏推荐
- Groovy之高级用法
- 目录权限错误导致 Oracle 11g rac 集群数据库无法启动的问题
- 探索C语言程序奥秘——C语言程序编译与预处理
- JS regular matching numbers, upper and lower case letters, underscores, midlines and dots [easy to understand]
- F - spices (linear basis)
- 【Proteus仿真】Arduino UNO+继电器控制照明设备
- 高速缓存Cache详解(西电考研向)
- PSQL column to row
- Is the compass reliable? Is it safe to open a securities account?
- AI服装生成,帮你完成服装设计的最后一步
猜你喜欢

业务与技术双向结合构建银行数据安全管理体系

折叠屏将成国产手机分食苹果市场的重要武器

Of the seven levels of software testers, it is said that only 1% can achieve level 7

Application of TSDB in civil aircraft industry

Folding screen will become an important weapon for domestic mobile phones to share the apple market

保险APP适老化服务评测分析2022第06期

3年测试经验,连简历上真正需要什么都没搞明白,张口就要20k?

It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets

random list随机生成不重复数

Exploring the mystery of C language program -- C language program compilation and preprocessing
随机推荐
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career
NPM package publishing tutorial
jwt
What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?
The ecosystem of the yuan universe
元宇宙的生态圈
Resolution of cross reference in IDA
Qt中使用QDomDocument操作XML文件
E - Average and Median(二分)
3年测试经验,连简历上真正需要什么都没搞明白,张口就要20k?
Kaggle 专利匹配比赛金牌方案赛后总结
常用的软件测试工具清单,请查收。
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (4) -- modify the scanip of Oracle11g RAC cluster
做软件安全测试的作用,如何寻找软件安全测试公司出具报告?
Call system function security scheme
Jetson Nano 从入门到实战(案例:Opencv配置、人脸检测、二维码检测)
指南针靠谱吗?开证券账户安全吗?
Groovy之高级用法
都2022年了,你还不了解什么是性能测试?