递归解法
走迷宫的递归走法很好理解:假设我们已经写好了搜路径的函数dfs
。那么我们只需要建立相邻两个状态的递推关系就行。递推关系很简单:已搜长度为n时,在当前走到的点上搜另外四个方向,每搜到一个可行方向,则使用n+1时的dfs
。
需要注意的有两点:
- 开头需要将起点放入记录路径的数组中。
- 起点需要设为不可通行(否则会出现很多经过两边起点的路径)。
- 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;
}