#include"stdio.h"
#include"malloc.h"
#define MAX 10
#define L 10
#define C 10
int sum[L][C];
typedef struct
{
int i;
int j;
}postype;
typedef struct
{
int ort;
postype seat;
int di;
}selemtype;
typedef struct
{
selemtype *base;
selemtype *top;
int sizemax;
}stack;
int initstack(stack *st)
{
st->base=(selemtype *)malloc(MAX*sizeof(selemtype));
if(!st->base) return 0;
st->top=st->base ;
st->sizemax=MAX;
return 1;
}
int empty(stack *st)
{
if(st->top-st->base>=st->sizemax)
return 0;
else if(st->base==st->top)
return 1;
return 3;
}
int push(stack *st,selemtype *e)
{
if(empty(st)==0)
{
st->base =(selemtype *)realloc(st->base,(st->sizemax+3)*sizeof(selemtype));
if(!st->base) return 0;
st->sizemax +=3;
}
*(st->top)=*e;
st->top++;
return 1;
}
int pop(stack *st,selemtype *e)
{
if(empty(st)==1)
return 0;
*e=*(st->top-1);
st->top--;
return 1;
}
void print(stack *outst)
{
selemtype post1;
stack outst1;
initstack(&outst1);
while(true)
{
if(pop(outst,&post1)==1)
push(&outst1,&post1);
else break;
}
while(true)
{
if(pop(&outst1,&post1)==1)
printf("%d,%d,%d,%d/n",post1.ort,post1.seat.i,post1.seat.j,post1.di);
else break;
}
}
void initsum()
{
int i=0,j=0;
for(i=0;i<L;i++)
for(j=0;j<C;j++)
sum[i][j]=0;
for(j=0,i=0;j<10;j++) sum[i][j]=1;
for(i=9,j=0;j<10;j++) sum[i][j]=1;
for(j=0,i=0;i<10;i++) sum[i][j]=1;
for(j=9,i=0;i<10;i++) sum[i][j]=1;
for(i=1,j=3;i<3;i++)sum[i][j]=1;
for(i=1,j=7;i<3;i++) sum[i][j]=1;
sum[4][2]=1;sum[4][3]=1;sum[4][4]=1;
sum[3][5]=1;sum[3][6]=1;sum[5][4]=1;
sum[6][2]=1;sum[6][6]=1;
for(i=7,j=2;j<5;j++) sum[i][j]=1;
sum[7][6]=1;sum[7][7]=1;sum[8][1]=1;
sum[1][0]=0;sum[8][9]=8;
}
void main()
{
int di=0,i=1,j=0,n;
int a=1,b=0;
stack outst;
selemtype post;
post.ort =0;
initsum();
initstack(&outst);
while(true)
{
a=i;b=j;
di=0;
if(sum[i][j]==8){ printf("按下路径走,你能成功出来了:/n");print(&outst);break;}
while(di<4)
{ a=i;b=j;
if(di==0) b=j+1;
if(di==1) a=i+1;
if((di==2)&&(j!=0))b=j-1;
if(di==3) a=i-1;
if((sum[a][b]==1)||(sum[a][b]==2)||(sum[a][b]==3))
{
di++;
}
else
{
sum[i][j]=2;
post.di=di;
post.ort++;
post.seat.i=i;
post.seat.j=j;
push(&outst,&post);
if(di==0) j++;
if(di==1) i++;
if(di==2) j--;
if(di==3) i--;
break;
}
}
if(di>=4)
{
sum[i][j]=3;
n=pop(&outst,&post);
if(n==1)
{
i=post.seat.i ;
j=post.seat.j;
}
else
{
printf("迷宫无法出去,是个死宫!");
break;
}
}
}
}