当前位置:网站首页>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});
}
}
}
}
边栏推荐
- Processon producer process (customized)
- PE file infrastructure sorting
- 計網 | 【四 網絡層】知識點及例題
- I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
- Are programmers from Huawei, Alibaba and other large manufacturers really easy to find?
- Intranet learning notes (7)
- yarn : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本
- 当一个接口出现异常时候,你是如何分析异常的?
- [I.MX6UL] U-Boot移植(六) 网络驱动修改 LAN8720A
- After reciting the eight part essay, I won the hemp in June
猜你喜欢

【FPGA】串口以命令控制温度采集

Transformers Roberta如何添加tokens

【STL源码剖析】STL六大组件功能与运用(目录)

LeetCode 210:课程表 II (拓扑排序)

The role of software security testing, how to find a software security testing company to issue a report?

高数 | 精通中值定理 解题套路汇总
![Planification du réseau | [quatre couches de réseau] points de connaissance et exemples](/img/c3/d7f382409e99eeee4dcf4f50f1a259.png)
Planification du réseau | [quatre couches de réseau] points de connaissance et exemples

Software testing salary in first tier cities - are you dragging your feet

What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?

It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
随机推荐
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (2) -- convert database to cluster mode
The Oracle 11g RAC cluster database cannot be started due to directory permission errors
ARM汇编中的栈桢小结
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(4)—— 修改 oracle11g rac 集群的 scanIP
会自动化—10K,能做自动化—20K,你搞懂自动化测试没有?
Is it out of reach to enter Ali as a tester? Here may be the answer you want
Can automate - 10k, can automate - 20K, do you understand automated testing?
对进程内存的实践和思考
当人们用互联网式的思维和视角来看待产业互联网的时候,其实已陷入到了死胡同
分布式事务解决方案和代码落地
How to uninstall CUDA
【FPGA】串口以命令控制温度采集
I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
[analysis of STL source code] functions and applications of six STL components (directory)
记一次beego通过go get命令后找不到bee.exe的坑
E - Average and Median(二分)
当一个接口出现异常时候,你是如何分析异常的?
How to monitor the log through the easycvr interface to observe the platform streaming?
Viewing MySQL password on Linux_ MySQL forgets password "suggestions collection" under Linux
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(3)—— 把数据库设置为归档模式