题目:应用栈实现迷宫游戏
要求:游戏运行时显示一个迷宫地图,玩家从地图左上角的入口处进入迷宫,从右下角出口离开迷宫。玩家不能穿墙而过。
输入描述:输入两个整数,分别表示二位数组的行数,列数,再输入相应的数组,其中的1表示墙壁,0表示可以走。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道
输出描述:左上角大右下角的最短路径
思路1:先判断能那个位置可以走,栈内储存做选择位置的坐标,如果选择过后出现死路径,则跳转回记录的选择位置的坐标,重新选择路径,往回走的方式就叫做回溯,采用最大深度泛式
对于二维数组的动态开辟,动态开辟二维数组首先需要开辟一个大小为N的指针数组,然后需要开辟N个大小为M的数组
以一个简单的[3,3]迷宫举例,运算逻辑为将走过的路径设置为2,避免之后返回已经走过的路径。随后在上下左右四个方向中选择可以行进的路径,到达新位置后如果未到达终点且不能再移动,则递归函数返回false,也就是将位置返回上一级判定,直至返回值有其他方向可走的位置。
到目前位置已经基本实现了迷宫寻路的功能,接下来需要解决的问题就是如何记录正确的路径,并将其输出出来。这里采用栈的形式进行储存,栈后进先出的特性,方便在进入错误路径后删除错误位置并且返回上一个位置。
栈的接口函数:
typedef PS STDataType;
typedef struct Stack
{
STDataType* a;//建立一个动态数组
STDataType top;//栈顶
STDataType capacity;//栈的容量
}ST;
//接口函数
void StackInit(ST* ps);//初始化
void StackDestory(ST* ps);//销毁
void StackPush(ST*ps,STDataType x);//入栈,不分头插尾插,因为栈只能在栈顶操作
void StackPop(ST* ps);//出栈
STDataType StackTop(ST* ps);//取栈顶数据
int StackSize(ST* ps);//得到栈的数据个数
bool StackEmpty(ST* ps);//判断栈是否为空
在栈的接口函数这里,将STDataType设置为ps,方便之后将结构体数据进行进栈出栈操作,这里也同样体现了采用typedef关键字的好处,避免面对不同的数据类型对函数反复重新定义
栈接口函数的实现:
void StackInit(ST* ps)
{
assert(ps);
ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
if (ps->a==NULL)
{
printf("malloc fail\n");
exit(-1);
}
ps->capacity = 4;
ps->top = 0;
//初始top=0,意味着top指向栈顶元素的下一个
//初始top=-1,意味着top指向栈顶元素
}
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackPush(ST* ps,STDataType x)
{
assert(ps);
if (ps->top==ps->capacity)
{
STDataType* tmp = (STDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(STDataType));
if (tmp==NULL)
{
printf("realloc fail\n");
exit(-1);//终止程序
}
else
{
ps->a = tmp;
ps->capacity *= 2;
}
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top>0);//空栈时调用top直接终止程序报错
ps->top--;//直接将栈顶数据删除,下一次有数据入栈会覆盖top位置
}
STDataType StackTop(ST* ps)//取栈顶元素
{
assert(ps);
assert(ps->top>0);
return ps->a[ps->top - 1];
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;//如果top为零,说明栈中没有元素,为空
//返回布尔值为1
}
代码实现:
//题目:应用栈实现迷宫游戏
//要求:游戏运行时显示一个迷宫地图,玩家从地图左上角的入口处进入迷宫,从右下角出口离开迷宫。玩家不能穿墙而过。
//输入描述:输入两个整数,分别表示二位数组的行数,列数,再输入相应的数组,其中的1表示墙壁,0表示可以走。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道
//输出描述:左上角大右下角的最短路径
//思路1:先判断能那个位置可以走,栈内储存做选择位置的坐标,如果选择过后出现死路径,则跳转回记录的选择位置的坐标,重新选择路径,往回走的方式就叫做回溯
//采用最大深度泛式
#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
typedef PS STDataType;
typedef struct Stack
{
STDataType* a;//建立一个动态数组
STDataType top;//栈顶
STDataType capacity;//栈的容量
}ST;
ST ps;//这里为ps栈的声明,用于之后存放路径位置
//接口函数
void StackInit(ST* ps);//初始化
void StackDestory(ST* ps);//销毁
void StackPush(ST* ps, STDataType x);//入栈,不分头插尾插,因为栈只能在栈顶操作
void StackPop(ST* ps);//出栈
STDataType StackTop(ST* ps);//取栈顶数据
int StackSize(ST* ps);//得到栈的数据个数
bool StackEmpty(ST* ps);//判断栈是否为空
void StackInit(ST* ps)
{
assert(ps);
ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
if (ps->a == NULL)
{
printf("malloc fail\n");
exit(-1);
}
ps->capacity = 4;
ps->top = 0;
//初始top=0,意味着top指向栈顶元素的下一个
//初始top=-1,意味着top指向栈顶元素
}
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
STDataType* tmp = (STDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(STDataType));
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);//终止程序
}
else
{
ps->a = tmp;
ps->capacity *= 2;
}
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);//空栈时调用top直接终止程序报错
ps->top--;//直接将栈顶数据删除,下一次有数据入栈会覆盖top位置
}
STDataType StackTop(ST* ps)//取栈顶元素
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;//如果top为零,说明栈中没有元素,为空
//返回布尔值为1
}
//定义一个坐标位置结构体
typedef struct postion
{
int row;
int col;
}PS;
//输出迷宫
void print(int **maze,int N,int M)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
printf("%d ", maze[i][j]);
}
printf("\n");
}
}
void PrintPath(ST *ps)//实现路径坐标输出
{
ST returnpath;
StackInit(&returnpath);
while (!StackEmpty(&ps))
{
StackPush(&returnpath, StackTop(&ps));
StackPop(&ps);
}
while (!StackEmpty(&returnpath))
{
PS top = StackTop(&returnpath);
printf("(%d,%d)\n",top.row,top.col);
StackPop(&returnpath);
}
StackDestory(&returnpath);
}
//判断上下左右是否可走
bool IsPass(int** maze,int N,int M,PS pos)
{
if (pos.row>=0&&pos.col>=0&&pos.row<N&&pos.col<M&&maze[pos.row][pos.col]==0)
{
return true;
}
else
{
return false;
}
}
//采用递归算法实现移动
bool GetMazePath(int** maze, int N, int M, PS cur)//cur代表当前位置
{
StackPush(&ps, cur);//将cur入栈
//判断是否到达终点
if (cur.row == N - 1 && cur.col == M - 1)
{
return true;
}
//探测cur的上下左右四个方向
PS next;
maze[cur.row][cur.col] = 2;//将走过的路径定义为2,避免在探测的过程中返回
//上
next = cur;//这一步的动作是为了给next重新赋值
next.row -= 1;
if (IsPass(maze, N, M, next))
{
if(GetMazePath(maze, N, M, next))
{
return true;
}
}
//下
next = cur;
next.row += 1;
if (IsPass(maze, N, M, next))
{
if (GetMazePath(maze, N, M, next))
{
return true;
}
}
//左
next = cur;
next.col -= 1;
if (IsPass(maze, N, M, next))
{
if (GetMazePath(maze, N, M, next))
{
return true;
}
}
//右
next = cur;
next.col += 1;
if (IsPass(maze, N, M, next))
{
if (GetMazePath(maze, N, M, next))
{
return true;
}
}
StackPop(&ps, cur);//当要走到return false时,说明这一步错误,需要出栈
return false;
}
int main()
{
int N = 0, M = 0;
printf("输入N M的值\n");
scanf_s("%d%d",&N,&M);
//int a[n][m]; 变长数组,部分编译器不支持
while (N!=NULL&&M!=NULL)
{
//动态开辟二维数组,首先需要开辟一个大小为N的指针数组,然后需要开辟N个大小为M的数组
int** maze = (int**)malloc(sizeof(int*) * N);
for (int i = 0; i < N; i++)
{
maze[i] = (int*)malloc(sizeof(int) * M);
}
//二维数组的输入
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
printf("输入迷宫\n");
scanf_s("%d", &maze[i][j]);
}
}
StackInit(&ps);//在栈的定义后设置了ps用于存放路径位置,这里要将栈初始化操作
//print(maze, N, M);
PS entry = { 0,0 };//左上角进入位置为{0,0}
if (GetMazePath(maze, N, M, entry))
{
PrintPath(&ps);
}
else
{
printf("为找到通路\n");
}
StackDestory(&ps);
for (int i = 0; i < N; i++)
{
free(maze[i]);
}
free(maze);
maze = NULL;
}
return 0;
}