迷宫问题 (广搜并记录路径)

迷宫问题

定义一个二维数组: 

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)

AC代码:

#include<iostream>
#include<cstdio> 
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;


int maze[15][15];
int vis[15][15];
int aa[50],bb[50];
int d[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int kx=0,ky=0,ex=4,ey=4;    //开始坐标为(0,0),结束坐标为(4,4) 
int len;


struct node{
	int x,y,step;		 
}a,next,path[105][105];  //a为当前点,next为下一个点,
//path记录路径 path[][].step记录此点是否已经走过,未走过:0,已走过:1 


int check(int s,int t)
{
	if(s<0 || s>4 || t<0 || t>4)	
		return 0;
	if(maze[s][t]==1)
		return 0;
//	if(vis[s][t]==1)          可以用path[][]数组来记录访问过的点,所以不再单独判断是否访问过 
//		return 0;
	return 1;
}


void shuchu() {             //从后向前寻找访问过的点,若正向输出,先存储后输出即可 
    int X,Y,x1,y1;
    int i=0;
    X=ex;
    Y=ey;
    aa[i]=X;bb[i]=Y;
    i++;
    while(i!=len) {
        x1=path[X][Y].x;
        y1=path[X][Y].y;
        X=x1;
        Y=y1;
        aa[i]=X;bb[i]=Y;
        i++;
    }
    for(i=len;i>=0;i--){
    	printf("(%d, %d)\n",aa[i],bb[i]);
    }
}


void bfs()
{
	queue<node> q;
	a.x=kx;a.y=ky;a.step=0;
//	vis[kx][ky]=1;
	q.push(a);
	while(!q.empty()){
		a=q.front();
		q.pop();       		
		for(int l=0;l<4;l++){
			next.x=a.x+d[l][0];
			next.y=a.y+d[l][1];
			if(!check(next.x,next.y) || path[next.x][next.y].step==1)				
				continue;
			next.step=a.step+1;
			path[next.x][next.y].step=1;        //记录是否走过 
            path[next.x][next.y].x=a.x;			//到达当前点的前一点的x坐标 
            path[next.x][next.y].y=a.y;			//到达当前点的前一点的y坐标 
	//		vis[next.x][next.y]=1;	                 
			q.push(next);
			if(next.x==ex && next.y==ey){
				len=next.step;		//当前步数 
        	    shuchu();
        	    return ;
			}   
		}
	}	
}


int main()
{
//	memset(vis,0,sizeof(vis));
	memset(path,0,sizeof(path));
	for(int i=0;i<5;i++){
		for(int j=0;j<5;j++){
			cin>>maze[i][j];
		}
	}
	path[kx][ky].step=0;
	bfs();
	return 0;
}



 

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/zhangxiaoduoduo/article/details/78127347
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞