一、基础知识
二、代码实现
#include<stdio.h>
#include<stdlib.h>
#define SIZE 7
#define MAXSIZE 1024
/*int Maze[SIZE][SIZE]={
{1,1,1,1,1,1,1},
{1,0,0,1,0,0,1},
{1,0,0,1,0,1,1},
{1,0,0,0,0,0,1},
{1,0,1,0,1,0,1},
{1,0,1,0,1,0,1},
{1,1,1,1,1,1,1}
};*/
typedef struct//路径方块的数据结构
{
int row;
int column;
int direction;
}SElemType;
typedef SElemType elemtype;//定义顺序栈中保存路径方块的数据类型
typedef struct SequenStack
{
elemtype data[MAXSIZE];
int top;
}SequenStack;
typedef struct//路径方块查找方向的数据结构
{
int x;
int y;
}Direction;
Direction Move[5]={{0,0},{0,1},{1,0},{0,-1},{-1,0}};//当前路径方块按照东西南北四个方向依次查找下一个方块
int path(SequenStack *s,int **Maze,Direction Move[5]);
int Push_SequenStack(SequenStack *s,elemtype x);
int GetTop_SequenStack(SequenStack *s,elemtype *x);
int SequenStack_Empty(SequenStack *s);
int Pop_SequenStack(SequenStack *s,elemtype *x);
SequenStack *Init_SequenStack();
int main()
{
SequenStack *s;
int i,j,**a,*b,**Maze;//用指针实现二维数组的变长定义,使用malloc手动分派空间,然后在实行赋值
s=Init_SequenStack();
if(s==NULL)
{
printf("申请顺序栈失败,程序结束!\n");
return;
}
SElemType tempprint;
int arow,acolumn;
printf("请输入迷宫行数:\n");
scanf("%d",&arow);
printf("请输入迷宫列数:\n");
scanf("%d",&acolumn);
//int Maze[arow][acolumn]={ }; c或者c++必须在对数组定义时确定数组的长度,所以不符合题意 因此使用 二级指针,分派空间,然后赋值
a=(int **)malloc(sizeof(int *) *arow);//???
b=(int *)malloc(sizeof(int) *arow);
for(i=0;i<arow;i++)
{
*(a+i)=(int *)malloc(sizeof(int) *acolumn);
/*for(j=0;j<acolumn;j++)
{
}*/
}
printf("请按行的顺序依次输入数字化迷宫(包括墙壁):\n");
for(i=0;i<arow;i++)
{
for(j=0;j<acolumn;j++)
{
scanf("%d",&a[i][j]);
}
}
Maze=a;
//检验函数输出
printf("检验迷宫数组是否输入正确\n");
for(i=0;i<arow;i++)
{
for(j=0;j<acolumn;j++)
{
printf("%d ",Maze[i][j]);
}
printf("\n");
}
path(s,Maze,Move);/*查找路径代码*/
return 0;
}
int path(SequenStack *s,int **Maze,Direction Move[5])//书上是寻找maze1、1到maze-2、maze-2的路
{
SElemType temp,tempprint;//求解迷宫问题的临时变量
SElemType tryPath;
int x,y,d,i,j,tryTime;
int br,bc,fr,fc;
printf("请输入迷宫起点:\n");
scanf("%d,%d",&br,&bc);
printf("请输入迷宫终点:\n");
scanf("%d,%d",&fr,&fc);
temp.row=br;
temp.column=bc;
temp.direction=0;
Push_SequenStack(s,temp);//第一个方块入栈
while(!SequenStack_Empty(s))//通过循环来查找从入口到出口的路径
{
GetTop_SequenStack(s,&temp);//获取栈顶的路径方块
x=temp.row;//把栈顶的temp里的行列数值赋给x,y
y=temp.column;
d=temp.direction+1;//设置当前路径的查找方向
tryTime=0;//当前路径方块的查找次数
while(d<=4)
{
i=x+Move[d].x;
j=y+Move[d].y;
if(Maze[i][j]==0)//如果当前位置可以通过,否则d++继续探查
{
if(tryTime==0)
{
Pop_SequenStack(s,&tryPath);
tryPath.direction=d;
Push_SequenStack(s,tryPath);
tryTime++;
}
temp.row=i;//设置新的可以通过的路径方块
temp.column=j;
temp.direction=1;
Push_SequenStack(s,temp);//将新的路径方块入栈
x=i;
y=j;
Maze[x][y]=-1;//迷宫中留下足迹
if(x==fr&&y==fc)//判断是否为出口 ,是则返回,完成查找
{
if(SequenStack_Empty(s))/*下边为输出路径代码*/
printf("\n此迷宫无路径,请重新调整参数\n");
else
{
while(!SequenStack_Empty(s))
{
Pop_SequenStack(s,&tempprint);
if(tempprint.row==br&&tempprint.column==bc)
{
printf("(%d,%d)",tempprint.row,tempprint.column);
}
else
{
printf("(%d,%d)<-",tempprint.row,tempprint.column);
}
}
}
return 1;
}
else
{
d=1;//设置新的路径查找方向继续探查
}
}
else//这是尝试不可通过的else
{
d++;
tryTime=0;
}
}
if(d==5&&!SequenStack_Empty(s))//如果此路径在四个方向上都探查过,则为死路,删除此路径方块
{
Pop_SequenStack(s,&temp);
Maze[x][y]='X';//迷宫中标记不可通
}
}
return 0;
}
SequenStack *Init_SequenStack()
{
SequenStack *S;
S=(SequenStack *)malloc(sizeof(SequenStack));
if(S==NULL)
return S;
S->top=-1;
return S;
}
int Push_SequenStack(SequenStack *S,elemtype x)
{
if(S->top>=MAXSIZE-1)
{
return 0;
}
S->top++;
S->data[S->top]=x;
return 1;
}
int Pop_SequenStack(SequenStack *S,elemtype *x)
{
if(S->top==-1)
{
return 0;
}
else
{
S->top--;
*x=S->data[S->top+1];
return 1;
}
}
int GetTop_SequenStack(SequenStack *S,elemtype *x)
{
if(S->top==-1)
{
return 0;
}
else
{
*x=S->data[S->top];
return 1;
}
}
int SequenStack_Empty(SequenStack *S)
{
if(S->top==-1)
{
return 1;
}
else
{
return 0;
}
}
这些都是一年前刚刚会一点点c语言写的,好多格式、思路地方都有待改进,希望看到的有什么建议,能直接给小菜鸡指出来,先谢谢大佬们了呀!