POJ-3984-迷宫问题

迷宫问题
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11452 Accepted: 6834

Description

定义一个二维数组:
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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

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

Sample Output

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

 

 

问题描述:从左上角开始,到右下角,输出最短的 路径

遇见的第一道记录路径的bfs,写了好多天才学会,不过这道是最最简单的一题.

为了记录路径,在结构体里要再加入两项prex,prey和一个结构体二维数组path来记录它的上一点是什么

用优先队列保证路径最短,用栈逆向存储走过的路径,很简单却很综合

#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)//根据结构体里的prex,prey逆向找到起点,并存进栈里,输出就成正序了
		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;
}


 

其实对于这个只有5*5的图来说,还有一种独特的解法;可以使用模拟队列,很巧妙,贴出来共赏

广搜的实现必定使用队列,而队列的特性是出对列后用过的元素就删去了,因而很难保存路径,为了保存,查找路径增加了很多麻烦.而这个模拟的队列拿两个变量front和rear保存分别记录数组里充当队列部分的头,尾位置,尾rear+1代表元素进队列,front+1代表队头元素出队.而之前的元素得以保留,方便了回头查询路径,精简了代码

 

//模拟队列 
#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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值