您的位置:首页 > 其它

迷宫问题(广搜 bfs)

2015-08-07 16:10 399 查看

迷宫问题

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 63   Accepted Submission(s) : 38
[align=left]Problem Description[/align]
定义一个二维数组:
int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};


它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
 

 

[align=left]Input[/align]
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
 

 

[align=left]Output[/align]
左上角到右下角的最短路径,格式如样例所示。
 

 

[align=left]Sample Input[/align]

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

 

 

[align=left]Sample Output[/align]

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

 

广搜bfs,需要记录路径,每逢搜索膝盖疼TAT

 

解法一:

#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
struct Node
{
int x, y, step, prex, prey;
friend bool operator < (Node a, Node b)
{
return a.step > b.step;
}
};
Node path[5][5];//记录第一次到坐标x, y 的节点
int map[5][5];//图
int vis[5][5];//该点是否查询过
int move[4][2] = {0,1, 0,-1, 1,0, -1,0};
bool judge(int x, int y)
{
return x >= 0 && x < 5 && y >= 0 && y < 5 && !map[x][y] && !vis[x][y];
}
void findpath(int ex, int ey)
{
stack<Node> rec;//用栈记录
Node now;
now = path[ex][ey];//从最后节点开始 向前找
rec.push(now);
while(1)
{
now = path[now.prex][now.prey];
rec.push(now);
if(now.x == 0 && now.y == 0)
break;
}
while(!rec.empty())
{
now = rec.top();
rec.pop();
printf("(%d, %d)\n", now.x, now.y);
}
}
void BFS()
{
priority_queue<Node> Q;
Node now, next;
now.x = 0;
now.y = 0;
now.step = 0;
Q.push(now);
while(!Q.empty())
{
now = Q.top();
Q.pop();
if(now.x == 4 && now.y == 4)
{
path[now.x][now.y] = now;//记录
break;
}
for(int k = 0; k < 4; k++)
{
next.x = now.x + move[k][0];
next.y = now.y + move[k][1];
next.step = now.step + 1;
if(judge(next.x, next.y))
{
vis[next.x][next.y] = 1;
next.prex = now.x;//记录前一路径
next.prey = now.y;
Q.push(next);
path[next.x][next.y] = next;//记录前一节点
}
}
}
findpath(4, 4);
}
int main()
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
scanf("%d", &map[i][j]);
}
memset(vis, 0, sizeof(vis));
BFS();
return 0;
}

 
 
另外一种是自己模拟了队列,但不删除队头元素,所以很容易输出记录的路径(貌似不能保证最优):
 
 
#include<stdio.h>
#include<iostream>
using namespace std;
int map[5][5];
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
int front = 0,rear = 1;
struct ndoe
{
int x,y,pre;
} q[100];
void print(int i)
{
if(q[i].pre!=-1)
{
print(q[i].pre);
printf("(%d, %d)\n",q[i].x,q[i].y);
}
}
void bfs(int x1,int y1)
{
q[front].x = x1;
q[front].y = y1;
q[front].pre = -1;
while(front < rear)//当队列不空
{
for(int i=0;i<4;i++)//搜索可达的路径
{
int a=dx[i]+q[front].x;
int b=dy[i]+q[front].y;
if(a<0||a>=5||b<0||b>=5||map[a][b])//是否在迷宫内,是否可行
continue;
else
{
map[a][b] = 1;//走过的路做标记
q[rear].x = a;
q[rear].y = b;
q[rear].pre = front;
rear++;//入队
}
if( a == 4&&b == 4)
print(front);
}
front++;//出队
}
}
int main()
{
int i,j,k;
for(i = 0;i < 5;i++)
for(j = 0;j < 5;j++)
scanf("%d",&map[i][j]);
printf("(0, 0)");
printf("\n");
bfs(0,0);
printf("(4, 4)");
printf("\n");
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 bfs 广搜