迷宫问题(广搜 bfs)

迷宫问题

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 63   Accepted Submission(s) : 38
Problem 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,需要记录路径,每逢搜索膝盖疼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;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值