迷宫问题 【模拟队列BFS】【反向BFS】

定义一个二维数组:

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)

解题思路: 因为此题,结果是返回路径,所以要记录下之前所走的路径。方案一:用一个数组去模拟队列,这样让头尾指针移动,而不是删除掉对头元素,然后记录下上一步的指针,就可以返回路径了。 方案二:用数组直接去记录到达此格子的last上一个格子。

方案一:

#include <iostream>
//需要记录路线,所以利用一维数组+首尾指针来模拟队列 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int map[5][5];
int star,en; //start指向头部,end指向尾部 
int inq[5][5]; //是否入过队 
int x[4]={-1,1,0,0};
int y[4]={0,0,-1,1};
int ans[30];
struct node{
	int x,y,pre;  // pre表示上一个路径的指针 
}que[30],S,T,Node;
//1表示墙壁,0表示可以走的路

bool judge(int x,int y){
	if(x<0 || x>=5 || y<0 || y>=5 ) return false;
	if(map[x][y] == 1) return false;
	if(inq[x][y] == true) return false;
	return true;
} 

int bfs(){
	S.x=0,S.y=0,S.pre=-1;
	que[0] = S; //-1表示无上一个 
	inq[0][0]=true;
	star=0,en=1;
	while(star != en)
	{
		node top=que[star];
		int temp=star;
		star = star+1;
		if(top.x == 4 && top.y == 4 )
		return top.pre;
		for(int i=0;i<4;i++)
		{
			int newX=top.x+x[i];
			int newY=top.y+y[i];
			if(judge(newX,newY))
			{
				Node.x=newX,Node.y=newY;
				Node.pre=temp;
				que[en++] = Node;
				inq[newX][newY]= true;
			}
		}
    }
    return -1;
}

int main(int argc, char** argv) {
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
		{
			cin >> map[i][j];
		}
	}
	int t=bfs();
	int cnt=0;
	while(t!=-1)
	{
		ans[cnt++]=t;
		t=que[t].pre;
	}
	for(int i=cnt-1;i>=0;i--)
	{
		printf("(%d, %d)\n",que[ans[i]].x,que[ans[i]].y);
	}	
	printf("(4, 4)\n");
	return 0;
}

方案二:

#include <iostream>
#include <queue>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int map[5][5];
int inq[5][5];
struct node{
	int x,y;
}S,T,Node;
int last[25];  //记录此格子,是从哪个格子过来的
int x[4]={-1,1,0,0};
int y[4]={0,0,-1,1};

bool judge(int x,int y){
	if(x < 0 || x >4 || y < 0 || y > 4) return false;
	if(map[x][y] == 1) return false;
	if(inq[x][y] == true ) return false;
	return true;
} 

void bfs(){
	S.x=4,S.y=4,last[24]=-1;
	queue <node> q;
	q.push(S);
	inq[S.x][S.y] = true;
	while(!q.empty())
	{
		node top = q.front();
		q.pop();
		if(top.x == 0 && top.y == 0 )
		return ;
		for(int i = 0 ; i < 4 ; i++ )
		{
			int newX=top.x+x[i];
			int newY=top.y+y[i];
			if(judge(newX,newY))
			{
				Node.x=newX,Node.y=newY;
				last[newX*5+newY] = top.x*5+top.y;
				q.push(Node);
				inq[newX][newY]= true;
			}	
		}
	}
}

void printpath(int path){
	if( path == -1 ) return ;
    printf("(%d, %d)\n",path/5,path%5);
	printpath(last[path]);
}

int main(){
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
		{
			cin >> map[i][j];
		}
	}
	bfs();
	printpath(0);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值