版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38784098/article/details/78238337
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef int ElemType;
#define MaxSize 50
/*int mg[n1][n2]
#define n1 50//定义行范围
#define n2 50//定义列范围
void array(int g,int h) //以二维数组形式定义迷宫内容
{
int a,b;
for(a=0;a<g;a++)
{
for(b=0;b<h;b++)
{
scanf("%d",&mg[a][b]); //输入迷宫对应的数组数据
}
}
}*/
int mg[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}
};
typedef struct
{
int i;//当前方块的行号
int j;//当前方块的列号
int di;//di是下一相邻可走方位的方位号
}Box; //方块类型
typedef struct
{
Box data[MaxSize];
int top;
}StType;//顺序栈类型
void InitStack(StType *&st)
{
st=(StType *)malloc(sizeof(StType));
st->top=-1;
}//初始化栈
bool Push(StType *&st,Box e)
{
if(st->top==MaxSize-1)//判断是否会溢出
return false;
st->top++;
st->data[st->top]=e;
return true;
}//入栈
bool StackEmpty(StType *st)
{
return (st->top==-1);
}//判断栈是否为空
bool GetTop(StType *st,Box &e)
{
if(st->top==-1)
return false;
e=st->data[st->top];
return true;
}//取栈顶元素
bool Pop(StType *st,Box &e)
{
if(st->top==-1)
return false;
e=st->data[st->top];
st->top--;
return true;
}//出栈
void DestroyStack(StType *st)
{
free(st);
}//销毁栈
bool mgpath(int xi,int yi,int xe,int ye)
{
Box path[MaxSize];
Box e;
int i,j,di,i1,j1,k;
bool find;
StType *st;
InitStack(st);
e.i=xi;e.j=yi;e.di=-1;//设置e为入口
Push(st,e);//e进栈
mg[xi][yi]=-1;//将入口的迷宫值设为-1,避免重复
while(!StackEmpty(st))//栈不为空循环
{
GetTop(st,e);
i=e.i;j=e.j;di=e.di;//取栈顶方块e
if(i==xe&&j==ye)
{
printf("一条迷宫路径如下:\n");
k=0;
while(!StackEmpty(st))
{
Pop(st,e);
path[k++]=e;
}
while(k>=1)
{
k--;
printf("\t(%d,%d)",path[k].i,path[k].j);
if((k+2)%5==0)
printf("\n");
}
printf("\n");
DestroyStack(st);
return true;
}
find=false;
while(di<4&&!find)
{
di++;
switch(di)
{
case 0:i1=i-1;j1=j;break;
case 1:i1=i;j1=j+1;break;
case 2:i1=i+1;j1=j;break;
case 3:i1=i;j1=j-1;break;
}
if(mg[i1][j1]==0) find=true;
}
if(find)
{
st->data[st->top].di=di;
e.i=i1;e.j=j1;e.di=-1;
Push(st,e);
mg[i1][j1]==-1;
}
else{
Pop(st,e);
mg[e.i][e.j]=0;
}
}
DestroyStack(st);
return false;
}
int main()
{
if(!mgpath(1,1,8,8))
printf("该迷宫没有解!");
return 1;
#include<stdlib.h>
using namespace std;
typedef int ElemType;
#define MaxSize 50
/*int mg[n1][n2]
#define n1 50//定义行范围
#define n2 50//定义列范围
void array(int g,int h) //以二维数组形式定义迷宫内容
{
int a,b;
for(a=0;a<g;a++)
{
for(b=0;b<h;b++)
{
scanf("%d",&mg[a][b]); //输入迷宫对应的数组数据
}
}
}*/
int mg[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}
};
typedef struct
{
int i;//当前方块的行号
int j;//当前方块的列号
int di;//di是下一相邻可走方位的方位号
}Box; //方块类型
typedef struct
{
Box data[MaxSize];
int top;
}StType;//顺序栈类型
void InitStack(StType *&st)
{
st=(StType *)malloc(sizeof(StType));
st->top=-1;
}//初始化栈
bool Push(StType *&st,Box e)
{
if(st->top==MaxSize-1)//判断是否会溢出
return false;
st->top++;
st->data[st->top]=e;
return true;
}//入栈
bool StackEmpty(StType *st)
{
return (st->top==-1);
}//判断栈是否为空
bool GetTop(StType *st,Box &e)
{
if(st->top==-1)
return false;
e=st->data[st->top];
return true;
}//取栈顶元素
bool Pop(StType *st,Box &e)
{
if(st->top==-1)
return false;
e=st->data[st->top];
st->top--;
return true;
}//出栈
void DestroyStack(StType *st)
{
free(st);
}//销毁栈
bool mgpath(int xi,int yi,int xe,int ye)
{
Box path[MaxSize];
Box e;
int i,j,di,i1,j1,k;
bool find;
StType *st;
InitStack(st);
e.i=xi;e.j=yi;e.di=-1;//设置e为入口
Push(st,e);//e进栈
mg[xi][yi]=-1;//将入口的迷宫值设为-1,避免重复
while(!StackEmpty(st))//栈不为空循环
{
GetTop(st,e);
i=e.i;j=e.j;di=e.di;//取栈顶方块e
if(i==xe&&j==ye)
{
printf("一条迷宫路径如下:\n");
k=0;
while(!StackEmpty(st))
{
Pop(st,e);
path[k++]=e;
}
while(k>=1)
{
k--;
printf("\t(%d,%d)",path[k].i,path[k].j);
if((k+2)%5==0)
printf("\n");
}
printf("\n");
DestroyStack(st);
return true;
}
find=false;
while(di<4&&!find)
{
di++;
switch(di)
{
case 0:i1=i-1;j1=j;break;
case 1:i1=i;j1=j+1;break;
case 2:i1=i+1;j1=j;break;
case 3:i1=i;j1=j-1;break;
}
if(mg[i1][j1]==0) find=true;
}
if(find)
{
st->data[st->top].di=di;
e.i=i1;e.j=j1;e.di=-1;
Push(st,e);
mg[i1][j1]==-1;
}
else{
Pop(st,e);
mg[e.i][e.j]=0;
}
}
DestroyStack(st);
return false;
}
int main()
{
if(!mgpath(1,1,8,8))
printf("该迷宫没有解!");
return 1;
}
运行结果: