以栈S记录当前路径,栈顶存放的是当前路径上的最后一个通道块,纳入路径就是入栈,删除前一个通道块就是出栈。设当前位置为M,伪代码如下:
do{
if M可通{
将M插入栈顶。
if M是出口,则结束;
else 将M更新为M的右边位置。
}
else {
if 栈非空&&栈顶位置尚有其他方向没有探索
则将M更新为M的下一相邻块(以顺时针为正序)。
if 栈非空&&栈顶位置四周均已知不可通行
则删除栈顶位置,然后重新测试新的栈顶位置 直至 找到可通行块或栈空
}
}while(栈非空);
感觉这里排版寄了,可以看这个图片:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 8
typedef struct
{
int row;
int col;
} Position;
typedef struct
{
Position maze_stack[SIZE * SIZE];
int top;
} Stack;
void initStack(Stack* stack) {
stack->top = -1;
}
int isEmpty(Stack* stack) {
return stack->top == -1;
}
void push(Stack* stack, Position pos) { // 插入
stack->top++;
stack->maze_stack[stack->top] = pos;
}
void pop(Stack* stack) { // 删除
stack->top--;
}
Position top(Stack* stack) { // 返回栈顶元素
return stack->maze_stack[stack->top];
}
int isExit(Position pos) { // 是否到达出口
return pos.row == SIZE - 1 && pos.col == SIZE - 1;
}
int isValid(Position pos, int maze[SIZE][SIZE], int visited[SIZE][SIZE])
{ // 检查 ①行和列在迷宫范围内 ②对应迷宫方块可通行 ③未被访问过,来判断位置有效
if (pos.row >= 0 && pos.row < SIZE && pos.col >= 0 && pos.col < SIZE &&maze[pos.row][pos.col] == 1 && visited[pos.row][pos.col] == 0) {
return 1;
}
return 0;
}
void printPath(Stack* stack)
{
for (int i = 0; i <= stack->top; i++) {
printf("(%d, %d)\n", stack->maze_stack[i].row, stack->maze_stack[i].col);
}
}
void solveMaze(int maze[SIZE][SIZE]) {
int visited[SIZE][SIZE] = { 0 };
Stack path;
initStack(&path);
Position start;
start.row = 0; start.col = 0;
push(&path, start); // 初始位置入栈
visited[start.row][start.col] = 1; // 设置初始位置已被访问过
while (!isEmpty(&path))
{
Position current = top(&path); // (实时更新)当前位置就是栈顶位置
if (isExit(current))
{
printf("Path to the exit:\n");
printPath(&path);
printf("\n");
return;
}
Position next; // 按右、下、左、上的顺序测试路线是否可行
next.row = current.row;
next.col = current.col + 1; // Move Right
if (isValid(next, maze, visited))
{
push(&path, next);
visited[next.row][next.col] = 1;
continue;
}
next.row = current.row + 1; // Move Down
next.col = current.col;
if (isValid(next, maze, visited))
{
push(&path, next);
visited[next.row][next.col] = 1;
continue;
}
next.row = current.row;
next.col = current.col - 1; // Move Left
if (isValid(next, maze, visited)) {
push(&path, next);
visited[next.row][next.col] = 1;
continue;
}
next.row = current.row - 1; // Move Up
next.col = current.col;
if (isValid(next, maze, visited)) {
push(&path, next);
visited[next.row][next.col] = 1;
continue;
}
// 如果所有方向都被探索或封锁,应该回溯(向前回溯一格),因为visited数组里存储了刚刚访问过的路径点(而此时已经判断此路径点的所有路径无效),所以不会重蹈覆辙地去走原来的错误的路线
pop(&path);
}
printf("No path found.\n"); // 彻底寄了
}
int main()
{
int maze[SIZE][SIZE] =
{
{1, 0, 1, 1, 1, 0, 0, 1},
{1, 0, 0, 0, 1, 0, 1, 1},
{1, 1, 1, 0, 1, 0, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 1, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1}
};
solveMaze(maze);
return 0;
}
输出(省略换行):
Path to the exit:(0, 0)(1, 0)(2, 0)(3, 0)(4, 0)(4, 1)(4, 2)(4, 3)(4, 4)(4, 5)(3, 5)(3, 6)(3, 7)(4, 7)(5, 7)(6, 7)(7, 7)
也就是:
本质就是什么也不管,就按右下左上的顺序往前冲,如果冲不动了就往前回溯一次,但还记得之前冲不动的地方,就知道不应该再去这个地方了。
可以在while循环里(isExit后,Position next前)加上以下代码来观察运行过程:
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
printf("%d ", visited[i][j]);
printf("\n");
}
printf("\n");