迷宫(蓝桥杯C/C++)dfs详解

题目描述
下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。

010000
000100
001001
110000

110000迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。

对于上面的迷宫,从入口开始,可以按 DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。

请注意在字典序中 D

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

运行限制
• 最大运行时间:1s
• 最大运行内存: 256M

题目过于复杂,接下来我将分为四步依次讲解,想直接看代码的直接点击目录哈~

第一步 简化迷宫,输出步数

我们先用dfs做,在迷宫中,建立坐标系,x轴正方向向下,y轴正方向向右,一共有上下左右四个方向,那么就可以建立三个数组,前两个表示在坐标中的位移,第三个表示方向,上下一一对应,比如R表示右,也就是x不变,y位移加一。

const int dirx[4]={0,0,1,-1};
const int diry[4]={1,-1,0,0};
const char dir[5]={'R','L','D','U'};

建立数组存迷宫,为了使起点为(1,1),补齐首行0,和首列0,所以row和col比实际的大1个

int maze[row+1][col+1]=
{
0,0,0,0,0,0,0,    
0,0,1,0,0,0,0,
0,0,0,0,1,0,0,
0,0,0,1,0,0,1,
0,1,1,0,0,0,0,
};

dfs(x,y,pos),pos为起点到x,y的步数,从(x,y)到(tox,toy)需要一步,所以起点到(tox,toy)需要pos+1步。

接下来的讲解都在代码注释中:

#include
#include
using namespace std;
const int dirx[4]={0,0,1,-1};
const int diry[4]={1,-1,0,0};
const char dir[5]={'R','L','D','U'};
//4行6列
const int row =4,col =6;
const int maxrc=6; 
//以左上角第一个位置为起点,往下x方向,往右y方向
//因为C语言的数组从0开始计,此处为了易于理解,从maze[1][1]存放迷宫
//maze内容如下
int maze[row+1][col+1]=
{
0,0,0,0,0,0,0,    
0,0,1,0,0,0,0,
0,0,0,0,1,0,0,
0,0,0,1,0,0,1,
0,1,1,0,0,0,0,
};
int best;//从起点到出口的最小步数
int mins[maxrc+5][maxrc+5];//存从起点到该点的步数 

bool judge(int x,int y) {//判断这个点是否越界,是否可以走。
	if(x>0&&x<=row&&y>0&&y<=col&&!maze[x][y])
		return true;//这个点没越界,而且可以走。 
	return false; 
}
void dfs(int x,int y,int pos) {
	if(pos>best)//如果当前路径已经走的步长pos大于之前可行方案最短路径长度 , 
		return ;//就无需沿着现有的路径继续走下去了,这个剪枝可以加快速度 
	if(x==row&&y==col) {//到达终点 
		if(pos<best) {//更短的路径记录下来
			best=pos; 
		}
		return ;
	}
	for(int i=0;i<4;i++) {//四个方向 
		int tox=x+dirx[i];//走向(tox,toy)位置 
		int toy=y+diry[i];
		if(judge(tox,toy)&&pos+1<=mins[tox][toy]) {
			//去往(tox,toy) 是合法的,没有越界,也是标记为0的可以行走的位置
			//并且当前的走法能够使得去到 (tox,toy) 步数更小 
			//就进入此if之内 
			maze[tox][toy]=1;//现在走到(tox,toy)了,走过了,就标记为障碍,那么下次就不能走了 
			mins[tox][toy]=pos+1;//当前的走法,到达(tox,toy)的步数比以前到过的步数更小
			//就把当前走法到达 (tox,toy)的步数更新 
			dfs(tox,toy,pos+1);//继续以(tox,toy,pos+1)深搜 
			maze[tox][toy]=0;//回溯找短的路,或者是字典序最小的。
			//恢复 (tox,toy)为0,即可行状态,好尝试其他位置在以后会走向它 
		}
	}
}
int main()
{
	memset(mins,0x3f,sizeof(mins));
	best=1<<28;
	//因为要求最小的,所以将mins数组和best初始化为很大的数 
	maze[1][1]=1;//标记起始点走过来,也就是变为障碍物,不再能走到它 
	dfs(1,1,1);//从起始点(1,1)1 开始深搜 
	cout<<best-1<<endl;// 因为bfs初始步数设为1,所以最后要减1 
	return 0;
}

有了这个代码,就可以把代码中的迷宫换成30行50列的迷宫,将row和col的值修改,可以得出步数。

第二步 加路径

定义string ans,temp和char a[row*col+5],在深搜的时候,数组a记录第pos步的方位,到达终点时,从1遍历到pos,把所有的方位连接到temp字符串中,如果pos

完整代码

#include
#include
using namespace std;
const int dirx[4]={0,0,1,-1};
const int diry[4]={1,-1,0,0};
const char dir[5]={'R','L','D','U'};
//4行6列
const int row =4,col =6;
const int maxrc=6; 
//以左上角第一个位置为起点,往下x方向,往右y方向
//maze内容如下
int maze[row+1][col+1]=
{0,0,0,0,0,0,0,    
0,0,1,0,0,0,0,
0,0,0,0,1,0,0,
0,0,0,1,0,0,1,
0,1,1,0,0,0,0,
};
int mins[maxrc+5][maxrc+5];
char a[row*col+5];
int best;
string ans;
bool judge(int x,int y) {//判断这个点是否越界,是否可以走。
	if(x>0&&x<=row&&y>0&&y<=col&&!maze[x][y])
		return true;//这个点没越界,而且可以走。 
	return false; 
}
void dfs(int x,int y,int pos) {
	if(pos>best)//如果当前路径已经走的步长pos大于之前可行方案最短路径长度 , 
		return ;//就无需沿着现有的路径继续走下去了,这个剪枝可以加快速度 
	if(x==row&&y==col) {//到达终点 
		string temp;//用temp把每次存的方向连接起来 
			for(int i=1;i<pos;i++)
				temp+=a[i];//路径 
		if(pos<best) {//更短的路径记录下来
			ans=temp;
			best=pos; 
		}
		else if(pos==best&&temp<ans)	ans=temp;//在实现路时最短的同时,保证字典序最小。
		return ;
	}
	for(int i=0;i<4;i++) {//四个方向 
		int tox=x+dirx[i];//走向(tox,toy)位置 
		int toy=y+diry[i];
		if(judge(tox,toy)&&pos+1<=mins[tox][toy]) {
			//去往(tox,toy) 是合法的,没有越界,也是标记为0的可以行走的位置
			//并且当前的走法能够使得去到 (tox,toy) 步数更小 
			//就进入此if之内 
			maze[tox][toy]=1;//现在走到(tox,toy)了,走过了,就标记为障碍,那么下次就不能走了 
			mins[tox][toy]=pos+1;//当前的走法,到达(tox,toy)的步数 比以前到过的步数更小
			//就把当前走法到达 (tox,toy)的步数更新 
			a[pos]=dir[i];//记录第pos步的方位 
			dfs(tox,toy,pos+1);//继续以(tox,toy,pos+1)深搜 
			maze[tox][toy]=0;//回溯找短的路,或者是字典序最小的。
			//恢复 (tox,toy)为0,即可行状态,好尝试其他位置在以后会走向它 
		}
	}
}
int main()
{

	memset(mins,1,sizeof(mins));
	best=1<<28;
	maze[1][1]=1;//标记起始点走过来,也就是变为障碍物,不再能走到它 
	dfs(1,1,1);//从起始点(1,1)1步 开始深搜 
	cout<<ans<<endl;
	cout<<best-1<<endl;
	return 0;
}

第三步 本题完整代码

#include
#include
using namespace std;
const int dirx[4]={0,0,1,-1};
const int diry[4]={1,-1,0,0};
const char dir[5]={'R','L','D','U'};
//4行6列
const int row =30,col =50;
const int maxrc=50; 
//以左上角第一个位置为起点,往下x方向,往右y方向
//因为C语言的数组从0开始计,此处为了易于理解和讲授,从maze[1][1]存放迷宫//左上角的内容。所以补齐首行0,和首列0,所以row和col比实际的大1个,//maze内容如下
int maze[row+1][col+1]=
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,1,0,0,0,1,1,0,1,1,1,0,1,0,1,0,1,1,0,1,1,1,1,0,0,0,
};
//int best;//从起点到出口的最小步数
//int mins[maxrc+5][maxrc+5];

int mins[maxrc+5][maxrc+5];
char a[row*col+5];
int best;
string ans;
bool judge(int x,int y) {//判断这个点是否越界,是否可以走。
	if(x>0&&x<=row&&y>0&&y<=col&&!maze[x][y])
		return true;//这个点没越界,而且可以走。 
	return false; 
}
void dfs(int x,int y,int pos) {
	if(pos>best)//如果当前路径已经走的步长pos大于之前可行方案最短路径长度 , 
		return ;//就无需沿着现有的路径继续走下去了,这个剪枝可以加快速度 
	if(x==row&&y==col) {//到达终点 
		string temp;
			for(int i=1;i<pos;i++)
				temp+=a[i];//路径 
		if(pos<best) {//更短的路径记录下来
			ans=temp;
			best=pos; 
		}
		else if(pos==best&&temp<ans)	ans=temp;//在实现路时最短的同时,保证字典序最小。
		return ;
	}
	for(int i=0;i<4;i++) {//四个方向 
		int tox=x+dirx[i];//走向(tox,toy)位置 
		int toy=y+diry[i];
		if(judge(tox,toy)&&pos+1<=mins[tox][toy]) {
			//去往(tox,toy) 是合法的,没有越界,也是标记为0的可以行走的位置
			//并且当前的走法能够使得去到 (tox,toy) 步数更小 
			//就进入此if之内 
			maze[tox][toy]=1;//现在走到(tox,toy)了,走过了,就标记为障碍,那么下次就不能走了 
			mins[tox][toy]=pos+1;//当前的走法,到达(tox,toy)的步数 比以前到过的步数更小
			//就把当前走法到达 (tox,toy)的步数更新 
			a[pos]=dir[i];//记录第pos步的方位 
			dfs(tox,toy,pos+1);//继续以(tox,toy,pos+1)深搜 
			maze[tox][toy]=0;//回溯找短的路,或者是字典序最小的。
			//恢复 (tox,toy)为0,即可行状态,好尝试其他位置在以后会走向它 
		}
	}
}
int main()
{

	memset(mins,1,sizeof(mins));
	best=1<<28;
	maze[1][1]=1;//标记起始点走过来,也就是变为障碍物,不再能走到它 
	dfs(1,1,1);//从起始点(1,1)1步 开始深搜 
	cout<<ans<<endl;
	//cout<
	return 0;
}

你可能感兴趣的:(蓝桥杯,蓝桥杯,深度优先,c语言,c++)