BFS+DFS走迷宫

【问题描述】 下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方。
010000
000100
001001
110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。
对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫, 一共 10 步。
其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),
请找出一种通过迷宫的方式, 其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。
请注意在字典序中D 在试题目录下有一个文件 maze.txt, 内容与下面的文本相同)

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

走迷宫求步数最少的路径:
1、从起点开始,通过BFS来一步一步地向外试探,直到遇到出口
2、在BFS的过程中还需要记录每次所到的下一个结点的前驱结点(例如从1走到2,即设置一个指针:2->1)
3、最后从终点开始DFS,每次都找前驱结点,最终遍历到起点就产生一条从终点到起点的路径,倒序输出即可。

思路跟Dijkstra+DFS打印最短路径有异曲同工之妙。


#include 
using namespace std;
struct node{	//结点 
	int x,y;	//结点坐标 
};
node pre[32][52];		//存前驱结点的二维数组 
char G[32][52];			//迷宫图 
int visit[32][52];		//访问标记数组 
int dx[4]={-1,0,0,1};		//设置x移动的方向
int dy[4]={0,-1,1,0};		//设置y移动的方向 
void bfs(int si,int sj){	//起点行、列坐标 
	node temp;				//结点 
	temp.x=si,temp.y=sj;	//设置起点的坐标 
	visit[temp.x][temp.y]=1;//标记[temp.x][temp.x]点已访问 
	queue<node>q;			//定义node型队列,存结点 
	q.push(temp);			//将起点入队 
	while(!q.empty()){		//当队列不为空 
		temp=q.front();		//取队首 
		q.pop();			//出队 
		node p;				//定义新的结点 
		for(int i=0;i<4;i++){
			p.x=temp.x+dx[i];	//设置结点p的x坐标 
			p.y=temp.y+dy[i];	//设置结点p的y坐标 
			if(G[p.x][p.y]!='1'&&visit[p.x][p.y]==0){	//试探是否可以访问 
				pre[p.x][p.y]=temp;		// 设置结点p的前驱结点为temp 
				visit[p.x][p.y]=1;		//标记已访问 
				q.push(p);				//出队 
			}
	}
}
vector<char>path;	//存路径的数组 
void dfs(node s){	//参数为结点 
	if(s.x==1&&s.y==1){	//如果遍历到起点,则路径已生成完毕 
		for(int i=path.size()-1;i>=0;i--){	//因为是从终点往前找的,所以输出的时候要从后往前输出 
			cout<<path[i];
		}
		return;
	}
	if(s.x==pre[s.x][s.y].x){	//如果x坐标相等 ,则移动的方向为L或R,否则移动方向为D或U 
		if(s.y-pre[s.x][s.y].y==1)	path.push_back('R');	//如果结点s的y坐标比前驱结点的y坐标大1,则表示移动的方向为R,否则为L 
		else path.push_back('L'); 
	}
	else{	//移动方向为U或D 
		if(s.x-pre[s.x][s.y].x==1)	path.push_back('D');
		else path.push_back('U');
	}
	dfs(pre[s.x][s.y]);//访问前驱结点 
}
int main(){ 
	fill(G[0],G[0]+32*52,'1');	//把迷宫初始化全为1,给迷宫做一层外墙 
	for(int i=1;i<=30;i++){		//输入真正的迷宫区域,行从1~30 
		for(int j=1;j<=50;j++){	//列从1~50 
			cin>>G[i][j];
		}
	}
	bfs(1,1);//对起点bfs 
	node s; 
	s.x=30,s.y=50;	//终点 
	dfs(s);//对终点dfs 
	return 0;
}

/**输入** 
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
*/

/**输出** 
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRUR
RUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLU
UUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDR
RDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDD
DDDDRR
*/ 

你可能感兴趣的:(蓝桥杯)