一、问题描述
给定一个n*m大小的迷宫,其中*代表不可通过的墙壁,而“.”代表平地,S表示起点,T代表终点。移动过程中,如果当前位置是(x,y)(下标从0开始),且每次只能前往上下左右(x,y+1)、(x,y-1)、(x-1,y)、(x+1,y)四个位置的平地,求从起点S到达终点T的最少步数。
` ` ` ` `
` * ` * `
` * S * `
` * * * `
` ` ` T *
在上面的样例中,S的坐标为(2,2),T的坐标为(4,3)。
二、代码实现
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=100;
struct Node
{
int x,y;
int step;
}S,T,node;
int n,m; //n行,m列
char maze[maxn][maxn];
bool inq[maxn][maxn]={false};
int X[4]={0,0,1,-1};
int Y[4]={1,-1,0,0};
bool test (int x,int y)
{
if(x>=n||x<0||y>=m||y<0) return false;
if(maze[x][y]=='*') return false;
if(inq[x][y]==true) return false;
return true;
}
int BFS()
{
queue<Node> q;
q.push(S);
whi

这篇博客介绍了如何求解一个n*m大小的迷宫中,从起点S到终点T的最少步数问题。在迷宫中,'*'表示墙壁,'.'表示可通行区域。移动规则是每次只能向上、下、左、右四个相邻的平地位置之一前进。文章提供了一个样例迷宫,并提出了求解这一问题的代码实现。
最低0.47元/天 解锁文章
879

被折叠的 条评论
为什么被折叠?



