黑马之王 - 2003-1-4 2:59:00
利用数据结构中的运行栈实现(里面函数参考《数据结构第二版》清华大学出版社)数据结构定义完全参考这本书。算法也参考这本书
#include<stdio.h>
#include<stdlib.h>
#define SIS 100/*运行栈一次分配的空间*/
#define N 10/*迷宫的最大坐标,根据迷宫大小修改*/
struct sit/*数据结构定义迷宫坐标*/
{
int seatx;
int seaty;
};
struct finds
{
int ord;/*路径标号*/
struct sit *seat;
int di;/*下一步试探的方向,上下左右用1,2,3,4来表示*/
};
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)/*把*find压入栈s,栈定指针加1*/
{
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)/*栈顶元素*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[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])/*判断改足迹是否走过,走过为1否则为0,进行下一步试探*/
{
int i,j;
i=curpos->seatx;
j=curpos->seaty;
if(a[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<n;i++)
scanf("%s",c);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
a[j]=c[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");
}
该被帖于【2003-1-5 0:02:08】被【黑马之王】修改
该被帖于【2003-1-5 0:13:30】被【黑马之王】修改
安德倚天剑 - 2005-1-12 19:23:00
还凑合。
haste - 2005-1-12 23:04:00
运行一下试试.
mfkwvfp - 2005-1-14 10:15:00
没试过啊不知道等我运行试一下了呵呵
gjinrong - 2005-1-21 22:48:00
强
zxdcool - 2005-2-1 22:37:00
不错的程序
美丽的星空 - 2005-2-5 22:12:00
唉,...
看不懂 真是郁闷
美丽的星空 - 2005-2-5 22:13:00
楼主 教教我怎么看好吗???
我的QQ是346431319
我很感兴趣 谢了
Charlie12345 - 2005-2-5 22:39:00
.........
ukm7895123 - 2005-2-6 10:03:00
我学过但从来没有想把它实现出来.
可怜的猫猫 - 2005-2-9 15:14:00
无语 这是我们的作业 。。。。早知道。。
开心笑笑 - 2005-2-12 15:05:00
对于我来说高难啊,谁教教我,呵呵
郁闷的豆子 - 2005-2-12 15:43:00
请问用的是什么编程工具?VC++好象不行,Borland C++ Builder?
intel - 2005-2-12 16:25:00
晕了
kuaihuolin - 2005-2-13 20:50:00
倒
可以的
我小交交我 - 2005-2-19 0:25:00
哇!
天上又冒星星了
我小交交我嘛,好吗
害羞妹妹 - 2005-2-19 17:22:00
呵呵,可以,好象我都做过,但只是类似的。
青羽碧翔 - 2005-2-19 18:03:00
对我来说太难了
高手教我啊
独孤失败007 - 2005-3-1 15:10:00
数据结构 科学出版社 C++实现
里面也有迷宫 好象比你的简单多了 是用 递归实现的 而且有8个方位 有兴趣的话可以看看
网事幽幽 - 2005-8-24 17:13:00
该用户帖子内容已被屏蔽
怪物125号 - 2005-8-24 22:36:00
我认为应该加一个图形界面,这样的程序输出一般要看N九才看得懂,输入也不方便。
快乐的橙 - 2006-8-20 11:49:00
用什么打開???
天下奇才 - 2006-8-20 15:55:00
用的是不是会塑算法?
© 2000 - 2026 Rising Corp. Ltd.