走迷宫的递归与非递归写法

这篇博客探讨了如何使用递归和非递归方法解决走迷宫问题。递归解法通过建立相邻状态的递推关系,先将起点设为不可通行并进行深度优先搜索。非递归解法则利用栈模拟递归,通过回溯寻找所有可能路径。两种方法都在C++中进行了实现,输出所有路径并统计总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

递归解法

走迷宫的递归走法很好理解:假设我们已经写好了搜路径的函数dfs。那么我们只需要建立相邻两个状态的递推关系就行。递推关系很简单:已搜长度为n时,在当前走到的点上搜另外四个方向,每搜到一个可行方向,则使用n+1时的dfs

需要注意的有两点:

  1. 开头需要将起点放入记录路径的数组中。
  2. 起点需要设为不可通行(否则会出现很多经过两边起点的路径)。
  3. dfs时,需要先将当前点设为不可通行,退出递归时,再需要将当前点设为可通行。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int maze[10][10]={
	{1,1,1,1,1,1,1,1,1,1},
	{1,0,0,1,0,0,0,1,0,1},
	{1,0,0,1,0,0,0,1,0,1},
	{1,0,0,0,0,1,1,0,0,1},
	{1,0,1,1,1,0,0,0,0,1},
	{1,0,0,0,1,0,0,0,0,1},
	{1,0,1,0,0,0,1,0,0,1},
	{1,0,1,1,1,0,1,1,0,1},
	{1,1,0,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1},	
};

int dir_x[] = {99, 0, 0, 1, -1};
int dir_y[] = {99, 1, -1, 0, 0};

typedef struct
{
	int x, y;
}Trace;

int sum=0; 

void printPath(Trace *trace, int n)
{
	for (int i = 0; i < n; ++ i)
	{
		printf("(%d,%d)", trace[i].x, trace[i].y);
	}
	cout << endl;
}

void dfs(int cur_x, int cur_y, int target_x, int target_y, int n, Trace *trace)
{
	if (cur_x == target_x && cur_x == target_y)
	{
		sum ++;
		printPath(trace, n);
	}
	else
	{
		int next_x, next_y;
		for (int i = 1; i <= 4; ++ i)
		{
			next_x = cur_x + dir_x[i];
			next_y = cur_y + dir_y[i];
			
			if (maze[next_x][next_y] == 0)
			{
				maze[next_x][next_y] = 1;
				dfs(next_x, next_y, target_x, target_y, n + 1, trace);
				maze[next_x][next_y] = 0;
			}
		}
	}
}

main()
{
	Trace trace[1000];
	int x1, y1, x2, y2;
	x1 = y1 = 1;
	x2 = y2 = 8;
	maze[x1][y1] = 1;
	dfs(x1, y1, x2, y2, 1, trace);
	cout << "total paths:" << sum << endl;
}


非递归解法

非递归本质是使用栈来模拟递归的实现过程。
需要对之前走过的路进行回溯,建立一个结构体数组trace[]和int数组choose[]。其中的trace中的每个元素都是一个代表坐标的二元组,这个结构体数组会记录我们探索的某一条路径的所有经过的点,也就是一个栈,top代表当前栈的元素个数(从1开始)。而choose[]数组是便于我们回溯的一个数组,如果我们将任何一个点可以做出的选择上下左右即为1,2,3,4,那么choose[i]就表示第i个点处前往第i+1点做出的动作编号。
主体部分是一个while循环,模仿八皇后的非递归写法,当起点选择的动作编号大于4时,循环终止,此时说明程序已经探索了所有可行的路径了。若当前栈顶元素恰好是目的地,那么打印输出trace,并将栈顶元素弹出,并将栈顶元素处设为可以通行;若不是目的地,则对栈顶元素从choose[top]+1到4搜索可行的动作编号,若搜到了,top++将新的点入栈,将新的点设为不可通行,并记录choose[top]。若没有搜到,则回溯,即弹出栈顶元素,将栈顶元素代表的点设为可通行,并将choose[top]设为0
当然当top==1时需要额外判断,top==1时,只需要对当前的choose[top]++,并将choose[top]设为0即可。

代码如下:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <mem.h> 
using namespace std;

int maze[10][10]={
	{1,1,1,1,1,1,1,1,1,1},
	{1,0,0,1,0,0,0,1,0,1},
	{1,0,0,1,0,0,0,1,0,1},
	{1,0,0,0,0,1,1,0,0,1},
	{1,0,1,1,1,0,0,0,0,1},
	{1,0,0,0,1,0,0,0,0,1},
	{1,0,1,0,0,0,1,0,0,1},
	{1,0,1,1,1,0,1,1,0,1},
	{1,1,0,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1},	
};

int dir_x[] = {99, 0, 0, 1, -1};
int dir_y[] = {99, 1, -1, 0, 0};

typedef struct
{
	int x, y;
}Trace;

int sum=0; 

void printPath(Trace *trace, int n)
{
	for (int i = 1; i <= n; ++ i)
	{
		printf("(%d,%d)", trace[i].x, trace[i].y);
	}
	cout << endl;
}

void solve_maze(int x1, int y1, int x2, int y2)
{
	int choose[1000];
	Trace trace[1000];
	
	memset(choose, 0, sizeof(int) * 1000);
	
	int top=1;	// 栈从1开始存储
	
	trace[top].x = x1;
	trace[top].y = y1;
	maze[x1][y1] = 1;	// 起点设为不可通行 
	
	while (choose[1] <= 4)
	{
		if (trace[top].x == x2 && trace[top].y == y2)
		{
			sum ++;
			printPath(trace, top);
			maze[trace[top].x][trace[top].y] = 0;
			top --;		// 当前搜索点出栈,开始回溯
		}
		else
		{
			int i, next_x, next_y;
			for (i = choose[top] + 1; i <= 4; ++ i)
			{
				next_x = trace[top].x + dir_x[i];
				next_y = trace[top].y + dir_y[i];
				
				if (maze[next_x][next_y] == 0)
					break;	
			}	
			
			if (i <= 4)
			{
				choose[top] = i;
				top ++;
				trace[top].x = next_x;
				trace[top].y = next_y;
				maze[next_x][next_y] = 1;
			}
			else
			{
				if (top == 1)
				{
					choose[top] ++;
					choose[top + 1] = 0;
				}
				else
				{
					maze[trace[top].x][trace[top].y] = 0;
					choose[top] = 0;
					top --; // 当前搜索点出栈,开始回溯 
				}
			}
		}
	}	
}


main()
{
	solve_maze(1, 1, 8, 8);
	cout << "total paths:" << sum << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值