老鼠走迷宫问题及其多个变种
一、说明
老鼠走迷宫问题的递归实现,是对递归思想的一种应用。
二、问题描述
给定一个二维数组,数组中2表示墙壁,0表示通路,由此数组可展示为一个迷宫图。给定入口位置和出口位置,判断之间是否存在通路并显示出走出迷宫的道路。
三、源码实现
/******************************************************************************************************
* function:老鼠走迷宫问题的递归解法,四周只有一个入口点,一个出口点
* author:yahai.zhang
* time: 2018.7.21
* File Name:老鼠走迷宫.c
******************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define ROW_MAX 21
#define COLUMN_MAX 36
int visit(int, int);
int startI = 0, startJ = 1; // 入口点
int endI = 20, endJ = 34; // 出口点
int success = 0;
int maze[ROW_MAX][COLUMN_MAX] = {
{2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2},
{2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2},
{2, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2},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 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2},
{2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2},
{2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2}
};
int main(void) {
printf("==========================BEGIN===========================\n");
int i, j;
printf("显示迷宫:\n");
for(i = 0; i < ROW_MAX; i++) {
for(j = 0; j < COLUMN_MAX; j++)
if(maze[i][j] == 2)
printf("█");
else
printf(" ");
printf("\n");
}
if(visit(startI, startJ) == 0)
printf("\n没有找到出口!\n");
else {
printf("\n显示路径:入口(%d, %d), 出口(%d, %d)\n", startI, startJ, endI, endJ);
for(i = 0; i < ROW_MAX; i++) {
for(j = 0; j < COLUMN_MAX; j++) {
if(maze[i][j] == 2)
printf("█");
else if(maze[i][j] == 1)
printf("◇");
else
printf(" ");
}
printf("\n");
}
}
printf("\n==========================END===========================\n");
return 0;
}
int visit(int i, int j) {
maze[i][j] = 1;
if(i == endI && j == endJ)
success = 1;
if(success != 1 && maze[i][j+1] == 0)
visit(i, j+1);
if(success != 1 && maze[i+1][j] == 0)
visit(i+1, j);
if(success != 1 && maze[i][j-1] == 0)
visit(i, j-1);
if(success != 1 && maze[i-1][j] == 0)
visit(i-1, j);
if(success != 1)
maze[i][j] = 0;
return success;
}
四、运行及结果
五、程序分析
该老鼠走迷宫主要依靠visit函数的递归实现,但是该实现存在以下几点不足:
- 迷宫只能存在一个入口和一个出口,否则程序运行出现问题
- 且入口点必须在边界点
- 出口点也必须在边界
六、程序改进
/******************************************************************************************************
* function:老鼠走迷宫问题的递归解法,四周只有多个入口点,多个出口点
* author:yahai.zhang
* time: 2018.7.21
* File Name:老鼠走迷宫.c
******************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define ROW_MAX 21
#define COLUMN_MAX 36
int visit(int, int);
int startI = 0, startJ = 1; // 入口点
int endI = 20, endJ = 34; // 出口点
int success = 0;
int maze[ROW_MAX][COLUMN_MAX] = {
{2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2},
{2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2},
{2, 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, 2, 0, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2},
{2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2},
{2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2},
{2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2}
};
int main(void) {
printf("==========================BEGIN===========================\n");
int i, j;
printf("显示迷宫:\n");
for(i = 0; i < ROW_MAX; i++) {
for(j = 0; j < COLUMN_MAX; j++)
if(maze[i][j] == 2)
printf("█");
else
printf(" ");
printf("\n");
}
printf("\n输入起点:");
scanf("%d %d", &startI, &startJ);
printf("输入终点:");
scanf("%d %d", &endI, &endJ);
if(visit(startI, startJ) == 0)
printf("\n没有找到出口!\n");
else {
printf("\n显示路径:入口(%d, %d), 出口(%d, %d)\n", startI, startJ, endI, endJ);
for(i = 0; i < ROW_MAX; i++) {
for(j = 0; j < COLUMN_MAX; j++) {
if(maze[i][j] == 2)
printf("█");
else if(maze[i][j] == 1)
printf("◇");
else
printf(" ");
}
printf("\n");
}
}
printf("\n==========================END===========================\n");
return 0;
}
int visit(int i, int j) {
maze[i][j] = 1;
if(i == endI && j == endJ)
success = 1;
if(success != 1 && j <= endJ && maze[i][j+1] == 0)
visit(i, j+1);
if(success != 1 && i <= endI && maze[i+1][j] == 0)
visit(i+1, j);
if(success != 1 && j >= 0 && maze[i][j-1] == 0)
visit(i, j-1);
if(success != 1 && i >= 0 && maze[i-1][j] == 0)
visit(i-1, j);
if(success != 1)
maze[i][j] = 0;
return success;
}
七、运行及结果
八、分析及改进
改进后,存在以下优点:
- 迷宫可以存在多个入口和多个出口
- 且入口点可以随意指定,出口位置随意指定
- 实现了任意两点的路径规划
在实际情况中,一般不知道出口的位置,所以在程序的运行过程中不该指定出口位置
九、程序改进
仅指定入口地址
/******************************************************************************************************
* function:老鼠走迷宫问题的递归解法,四周只有多个入口点,多个出口点,仅指定入口地址
* author:yahai.zhang
* time: 2018.7.21
* File Name:老鼠走迷宫.c
******************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define ROW_MAX 21
#define COLUMN_MAX 36
int visit(int, int);
int startI = 0, startJ = 1; // 入口点
int endI = 20, endJ = 34; // 出口点
int success = 0;
int maze[ROW_MAX][COLUMN_MAX] = {
{2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 2},
{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, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 0},
{2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 2},
{2, 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 0, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2},
{2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2},
{2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2},
{2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
};
int main(void) {
printf("==========================BEGIN===========================\n");
int i, j;
printf("显示迷宫:\n");
for(i = 0; i < ROW_MAX; i++) {
for(j = 0; j < COLUMN_MAX; j++)
if(maze[i][j] == 2)
printf("█");
else
printf(" ");
printf("\n");
}
printf("\n输入起点:");
scanf("%d %d", &startI, &startJ);
if(visit(startI, startJ) == 0)
printf("\n没有找到出口!\n");
else {
printf("\n显示路径:\n");
for(i = 0; i < ROW_MAX; i++) {
for(j = 0; j < COLUMN_MAX; j++) {
if(maze[i][j] == 2)
printf("█");
else if(maze[i][j] == 1)
printf("◇");
else
printf(" ");
}
printf("\n");
}
}
printf("\n==========================END===========================\n");
return 0;
}
int visit(int i, int j) {
maze[i][j] = 1;
if ( i == ROW_MAX || j == COLUMN_MAX)
success = 1;
if ((i == endI && j == endJ) )
success = 1;
if(success != 1 && j <= endJ && maze[i][j+1] == 0)
visit(i, j+1);
if(success != 1 && i <= endI && maze[i+1][j] == 0)
visit(i+1, j);
if(success != 1 && j >= 0 && maze[i][j-1] == 0)
visit(i, j-1);
if(success != 1 && i >= 0 && maze[i-1][j] == 0)
visit(i-1, j);
if(success != 1)
maze[i][j] = 0;
return success;
}
十、运行结果