题目来自《挑战程序设计竞赛》2.1最基础的穷竭搜索
1.题目描述
给定一个大小为N*M的迷宫,迷宫由通道和墙壁组成,每一步都可以向邻接的上下左右四个的通道移动。求从起点到终点所需要的最小步数。假定本题从起点一定可以到达终点。
2.思路与分析
宽度优先搜索按照距开始状态由近及远的顺序搜索,因此很容易解决最短路径、最少操作数之类的问题。本题中状态时所在目前为止的坐标,因此可以用pair或者编码成int来表示状态。当状态更复杂时就需要用类来表示了。转移的方式为四方向移动,状态数与迷宫的大小是相等的,时间复杂度为O(4*N*M)=O(N*M)。
3.代码
注:本文比原书多了一个输出路径,思路很简单,只需每次都记录上一个位置(父亲),然后利用递归就可以啦。而输出路径正是poj3984所要求的
/*
10 10
# S # # # # # # . #
. . . . . . # . . #
. # . # # . # # . #
. # . . . . . . . .
# # . # # . # # # #
. . . . # . . . . #
. # # # # # # # . #
. . . . # . . . . .
. # # # # . # # # .
. . . . # . . . G #
*/
#include<iostream>
#include<cstdio>
#include<queue>
#include<utility>
using namespace std;
typedef pair<int,int> P;
#define INF 0x7fffffff
#define maxn 105
#define maxm 105
char maze[maxn][maxm];
int sx,sy;//起点坐标
int gx,gy;//终点坐标
int N,M;
int d[maxn][maxm];//起点到各个位置的最短距离数组
P father[maxn][maxm];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
//求(sx,sy)到(gx,gy)的最短距离
int bfs()
{
queue<P> que;
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
d[i][j]=INF;
}
}
que.push(P(sx,sy));
d[sx][sy]=0;
while(que.size()){
P p=que.front();
que.pop();
if(p.first==gx&&p.second==gy) break;
//四个方向的循环
for(int i=0;i<4;i++){
int nx=p.first+dx[i];
int ny=p.second+dy[i];
//判断是否可以移动,以及是否已访问过
if(0<=nx&&nx<N&&ny>=0&&ny<M&&maze[nx][ny]!='#'&&d[nx][ny]==INF){
que.push((P(nx,ny)));
father[nx][ny]=p;
d[nx][ny]=d[p.first][p.second]+1;
}
}
}
return d[gx][gy];
}
void solve(P p)
{
if(p.first==sx&&p.second==sy){
cout<<"("<<sx<<","<<sy<<")"<<endl;
return;
}
else{
solve(father[p.first][p.second]);
cout<<"("<<p.first<<","<<p.second<<")"<<endl;
}
}
int main()
{
scanf("%d%d",&N,&M);
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
cin>>maze[i][j];
if(maze[i][j]=='S'){
sx=i;
sy=j;
}
if(maze[i][j]=='G'){
gx=i;
gy=j;
}
}
}
int res=bfs();
printf("%d\n",res);
P p(gx,gy);
solve(p);
return 0;
}
4.poj3984
题目链接题目
思路和书上的一样,直接贴代码了。
AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<utility>
using namespace std;
typedef pair<int,int> P;
#define INF 0x7fffffff
char maze[5][5];
int sx,sy;//起点坐标
int gx,gy;//终点坐标
int d[5][5];//起点到各个位置的最短距离数组
P father[5][5];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
//求(sx,sy)到(gx,gy)的最短距离
int bfs()
{
queue<P> que;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
d[i][j]=INF;
}
}
que.push(P(sx,sy));
d[sx][sy]=0;
while(que.size()){
P p=que.front();
que.pop();
if(p.first==gx&&p.second==gy) break;
//四个方向的循环
for(int i=0;i<4;i++){
int nx=p.first+dx[i];
int ny=p.second+dy[i];
//判断是否可以移动,以及是否已访问过
if(0<=nx&&nx<5&&ny>=0&&ny<5&&maze[nx][ny]!='1'&&d[nx][ny]==INF){
que.push((P(nx,ny)));
father[nx][ny]=p;
d[nx][ny]=d[p.first][p.second]+1;
}
}
}
return d[gx][gy];
}
void solve(P p)
{
if(p.first==sx&&p.second==sy){
cout<<"("<<sx<<", "<<sy<<")"<<endl;
return;
}
else{
solve(father[p.first][p.second]);
cout<<"("<<p.first<<", "<<p.second<<")"<<endl;
}
}
int main()
{
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cin>>maze[i][j];
}
}
sx=0;sy=0;
gx=4;gy=4;
int res=bfs();
//printf("%d\n",res);
P p(gx,gy);
solve(p);
return 0;
}

该博客介绍了一种使用宽度优先搜索(BFS)解决迷宫问题的方法,详细阐述了思路与分析,包括从起点到终点的最小步数计算。此外,博主还提供了额外的代码实现,以记录并输出路径,满足POJ3984比赛的要求。
6000

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



