迷宫的最短路径
题目描述
给定一个大小为 N×M的迷宫。迷宫由通道和墙壁组成,每一步可以向邻接的上下左右四格的通道移动。请求出从起点到终点所需的小步数。请注意,本题假定从起点一定可以移动到终点
限制条件:N,M<=100;
样例输入:N=10,M=10('#','.','S','G'分别表示墙壁,通道,起点和终点)
#S######.#
......#..#
.#.##.##.#
.#........
##.##.####
....#....#
.#######.#
....#.....
.####.###.
....#...G#
输出最短路径为22
#include <iostream>
#include <queue>
#include <memory.h>
#include <cmath>
using namespace std;
const int maxn=100+10;
char maze[maxn][maxn];
int steps[maxn][maxn];//表示走到坐标(x,y)处的最短距离
int m,n;
typedef pair<int,int> P;
int sx,sy,gx,gy;
int total=0;
void bfs(){
queue<P> q;
q.push(P(sx,sy));//将起点入队
steps[sx][sy]=0;//设置起点的步数为0
while(q.size()){
P p=q.front();
q.pop();//每次获取队列的第一个元素,并出队
if(p.first==gx&&p.second==gy) break; //如果到达终点或者队列为空,跳