“栈”是一个基本的数据结构,其原理是:先进后出。这个程序是我大学二年级的时候写的,当时学习数据结构老师要求的作业,不过这个确实是一个经典的程序。由于当时没有学习面向对象技术,所以采用struct来实现的。该程序在tc2,0(需要把//注释删除掉)和vc++6.0下面调试通过
#include < stdlib.h>
#include < stdio.h >
#define SIS 100
#define N 10
struct sit
{
int seatx;
int seaty;
};//当前访问的坐标
struct finds
{
int ord;
struct sit *seat;
int di;
};//di为下个寻找的方向
struct stack
{
struct finds *base;
struct finds *top;
int size;
};
void initstack(struct stack *s)
{
struct finds *link,*end;
s->base=s->top=link=(struct finds *)malloc(SIS*sizeof(struct finds));
end=link+SIS-1;
for(;link<=end;link++)
link->seat=(struct sit *)malloc(sizeof(struct sit));
s->size=SIS;
}//堆栈初始化
void push(struct stack *s,struct finds *find)
{
struct finds *link,*end;
if(s->top-s->base>=s->size)
{
s->base=(struct finds *)realloc(s->base,(s->size+SIS)*sizeof(struct finds));
link=s->base+s->size;
s->top=s->size+s->base;
s->size+=SIS;
end=s->base+s->size;
for(;link<=end;link++)
link->seat=(struct sit *)malloc(sizeof(struct sit));
}
s->top->ord=find->ord;
s->top->seat->seatx=find->seat->seatx;
s->top->seat->seaty=find->seat->seaty;
s->top->di=find->di;
s->top++;
}
void pop(struct stack *s,struct finds *find)
{
s->top--;
find->ord=s->top->ord;
find->seat->seatx=s->top->seat->seatx;
find->seat->seaty=s->top->seat->seaty;
find->di=s->top->di;
}
void footprint(int a[N][N],struct sit *seat)
{
int i,j;
i=seat->seatx;
j=seat->seaty;
a[i][j]=-1;
}
void nextpos(struct sit *find,struct sit *seat,int di)
{
int i,j;
switch(di)
{case 1:i=seat->seatx;j=seat->seaty+1;break;
case 2:i=seat->seatx+1;j=seat->seaty;break;
case 3:i=seat->seatx;j=seat->seaty-1;break;
case 4:i=seat->seatx-1;j=seat->seaty;break;
}
find->seatx=i;
find->seaty=j;
}
int pass(struct sit *curpos,int a[N][N])
{
int i,j;
i=curpos->seatx;
j=curpos->seaty;
if(a[i][j]==0)return 1;
else return 0;
}
print(struct stack *s)
{ do
{
printf("(%d,%d)",s->base->seat->seatx,s->base->seat->seaty);
s->base++;
}while(s->top!=s->base);
}
void main()
{
struct stack *s;
struct finds *find,*e;
struct sit *start,*end,*curpos;
int curstep=1,a[N][N],i,j,n,m,pa;
char c[N][N];
s=(struct stack *)malloc(sizeof(struct stack));
find=(struct finds *)malloc(sizeof(struct finds));
e=(struct finds *)malloc(sizeof(struct finds));
start=(struct sit *)malloc(sizeof(struct sit));
end=(struct sit *)malloc(sizeof(struct sit));
curpos=(struct sit *)malloc(sizeof(struct sit));
initstack(s);
puts("在使用前请仔细阅读使用说明");
while(1)
{
printf("请输入迷宫的长和宽(n,m)");
pa=scanf("%d,%d",&n,&m);
if(pa!=2||n>81||m>81||n<=0||m<=0)puts("输入数据错误,请重新输入");
else break;
}
printf("请输入你的迷宫(0代表路径,连续输入)/n");
for(i=0;i scanf("%s",c[i]);
for(i=0;i for(j=0;j a[i][j]=c[i][j]-48;
while(1)
{
printf("进入迷宫的位置(n,m)");
scanf("%d,%d",&curpos->seatx,&curpos->seaty);
if(curpos->seatx>81||curpos->seaty>81||curpos->seatx<=0||curpos->seaty<=0||curpos->seatx>=n||curpos->seatx>=m||curpos->seaty>=m||curpos->seaty>=n)puts("输入数据错误,请重新输入");
else break;
}
while(1)
{
printf("走出迷宫的位置(n,m)");
scanf("%d,%d",&end->seatx,&end->seaty);
if(end->seatx>81||end->seaty>81||end->seatx<=0||end->seaty<=0||end->seatx>=n||end->seatx>=m||end->seaty>=n||end->seaty>=m)puts("输入数据错误,请重新输入");
else break;
}
do
{
if(pass(curpos,a))
{footprint(a,curpos);
e->ord=curstep;
e->seat=curpos;
e->di=1;
push(s,e);
if(curpos->seatx==end->seatx&&curpos->seaty==end->seaty)break;
nextpos(curpos,e->seat,1);
curstep++;
}
else {
if(e->di<4){pop(s,e);e->di++,push(s,e);nextpos(curpos,e->seat,e->di);}
else if(e->di==4)
{
pop(s,e);pop(s,e);push(s,e);e->di++;
if(e->di>4)pop(s,e);
nextpos(curpos,e->seat,e->di);
}
}
}while(1);
puts("输出路径为");
print(s);
puts("/n");
}