当前位置:网站首页>Minimum number of steps to get out of the maze 2

Minimum number of steps to get out of the maze 2

2022-08-10 01:47:00 -JMY-

Title description

When you're standing in a maze, you tend to get disoriented by the intricate paths, and if you can get your hands on a map of the maze, it's really easy.Suppose you have obtained a drawing of an n*m maze, please find the shortest path from the starting point to the exit.

Enter

The first line is two integers n and m (1<=nm<=100), which represent the number of rows and columns of the maze.The next n lines, each with a string of length m, represent the layout of the entire maze.The characters '.' represent open spaces, '#' represent walls, 'S' represent starting points, and 'T' represent exits.

Output

Output the minimum number of steps required to travel from the starting point to the exit.

Sample input

3 3S#T.#....

Sample output

6

Reference code:

#include
using namespace std;
int n,m;
int q[5000][2],hh,tt,kx,ky,l,gx,gy;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int d[50][50];
char a[50][50];
void bfs(){
while(hh!=tt){
kx=q[hh][1];
ky=q[hh][0];
hh++;
for(int i=0;i<4;i++){
int xx=kx+dx[i];
int yy=ky+dy[i];
if(xx>=0&&xx=0&&yy tt++;
q[tt-1][0]=yy;
q[tt-1][1]=xx;
d[yy][xx]=d[ky][kx]+1;
    
cin>>n>>m;
for(int i=0;i for(int j=0;j cin>>a[i][j];
for(int i=0;i for(int j=0;j if(a[i][j]=='S'){
q[0][0]=i;
q[0][1]=j;
d[i][j]=1;
 
if(a[i][j]=='T'){
gx=j;
gy=i;
cout< return 0;
}

原网站

版权声明
本文为[-JMY-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208092346036620.html