看代码的时候突然想起来了以前学李戈老师的《C程序设计进阶》里面的这道题,然后在eclipse里找了找,翻到了原来写过的这个代码,重新又看了一遍。
感谢那个时候的自己,注释加了不少。这算是彼时最初接触编程的我遇到的第一道真正算法意义上的难题(当时我还没学数据结构),想了很久想不出来,然后上网搜,倒是搜到不少人的代码,可惜没有注释根本看不懂(有注释说不定也看不懂,毕竟要用广搜)。后来还是搜索迷宫类问题找到了一篇详细讲深搜广搜的帖子才勉强把代码试着撸了起来。
当初彻底搞懂之后的感想:
如今:
到POJ上重新找到了这题,提交了一下,嗯,AC。
附上题目和代码:
走出迷宫
总时间限制:
1000ms
内存限制:
65536kB
描述
当你站在一个迷宫里的时候,往往会被错综复杂的道路弄得失去方向感,如果你能得到迷宫地图,事情就会变得非常简单。
假设你已经得到了一个n*m的迷宫的图纸,请你找出从起点到出口的最短路。
输入
第一行是两个整数n和m(1<=n,m<=100),表示迷宫的行数和列数。
接下来n行,每行一个长为m的字符串,表示整个迷宫的布局。字符'.'表示空地,'#'表示墙,'S'表示起点,'T'表示出口。
输出
输出从起点到出口最少需要走的步数。
样例输入
3 3
S#T
.#.
...
样例输出
6
参考代码:
/*
* outOfTheMaze.cpp
*
* Created on: 2017年10月30日
* Author: Arno
*
*/
#include<iostream>
#include<queue>
using namespace std;
struct Node{
int x;
int y;//结点坐标
int step=0;
};
int n,m;
char maze[100][100];
//检查探索得到的下一步是否合法,若在数组范围内且可以通过的话就合法,否则非法
bool isValid(Node Vx){
if(Vx.x>=0&&Vx.x<n&&Vx.y>=0&&Vx.y<m&&maze[Vx.x][Vx.y]=='.')
return true;
else
return false;
}
//广度优先搜索算法
/*此算法涉及数据结构
* 详情请参考:
* http://blog.csdn.net/raphealguo/article/details/7523411
* 我就是在这个上面学的,感谢
*/
//Vs为开始结点,Vd为终止结点,传入函数
bool BFS(Node &Vs,Node &Vd){
queue<Node> Q;//辅助队列
Node Vn,Vw;//Vn为每次从队列中取出的队首,Vw为在Vn基础上探索的下一工作结点
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//四个方向,用二维数组作为坐标改变值探索当前坐标的上下左右四个方向
Q.push(Vs);//开始结点入队
maze[Vs.x][Vs.y]='#';//标记已访问,不再继续访问
//队列不空就继续搜索路径
while(!Q.empty()){
Vn=Q.front();//从队列队首取结点,赋给Vn
Q.pop();
for(int i=0;i<4;i++){
Vw.x=Vn.x+dir[i][0];//探寻当前结点的上下左右结点
Vw.y=Vn.y+dir[i][1];
if(Vw.x==Vd.x&&Vw.y==Vd.y){
Vd.step=Vn.step+1;//若当前结点的下一个结点即为终点,则让当前结点的步数加1赋给终点的步数
return true;
}
if(isValid(Vw)){
Vw.step=Vn.step+1;//若下一结点为合法结点且尚未被访问过,则将当前结点的步数加1赋给工作结点
Q.push(Vw);//工作结点入队,成为接下来待取的探索节点
maze[Vw.x][Vw.y]='#';//将此工作结点的位置标记已访问,不再继续访问
}
}
}
return false;
}
int main(){
cin>>n>>m;
struct Node start,end;//开始结点和终点
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>maze[i][j];
if(maze[i][j]=='S'){
start.x=i;
start.y=j;
}
else if(maze[i][j]=='T'){
end.x=i;
end.y=j;
maze[i][j]='.';//得到终点后要将其置为可以访问,满足访问条件
}
}
}
if(BFS(start,end)){
cout<<end.step<<endl;//输出终点步数,此时即为最短路径
}
else
cout<<"None"<<endl;
return 0;
}
/*
* 参考代码:
* http://www.cnblogs.com/huashanqingzhu/p/7243411.html
* 给了我很大的启发,感谢
*
#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
struct obj
{
int x,y;
int step;//表示从出发点到达(x,y)需要的步数
};
char a[102][102];
queue<struct obj> que;
struct obj start,destination;
int dx[4]={-1,0,1,0};//上右下左
int dy[4]={0,1,0,-1};
int main(int argc, char *argv[])
{
int n,m,i,j,xx,yy;
struct obj temp,temp2;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%s",a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]=='S') { start.x=i; start.y=j; start.step=0; }
else if(a[i][j]=='T') { destination.x=i; destination.y=j; a[i][j]='.'; }
//printf("%c",a[i][j]);
}
//printf("\n");
}
//printf("%d %d %d\n",start.x,start.y,start.step);
//printf("%d %d %d\n",destination.x,destination.y,destination.step);
a[start.x][start.y]='#';
que.push(start);
while(!que.empty())
{
temp=que.front();que.pop();
//printf("%d %d %d ",temp.x,temp.y,temp.step);
//printf("%d %d %d\n",destination.x,destination.y,destination.step);
if(temp.x==destination.x&&temp.y==destination.y)
{
destination.step=temp.step;
break;
}
for(i=0;i<4;i++)
{
xx=temp.x+dx[i];
yy=temp.y+dy[i];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]=='.')
{
a[xx][yy]='#';
temp2.x=xx;
temp2.y=yy;
temp2.step=temp.step+1;
que.push(temp2);
}
}
}
printf("%d\n",destination.step);
return 0;
}
*/