共有回帖数 0 个
-
#includestdio.h
#includemalloc.h
#includetime.h
#define NULL 0
struct maze
{
struct maze *prior;
int x;
int y;
int dir;
struct maze *next;
};
int cmp(struct maze *head,struct maze *q)
{
struct maze *p;
int i=1;
p=head;
while(p-x!=q-x||p-y!=q-y)
p=p-next;
if(p==q)
return(1);
else
return(0);
}
void main()
{
int a[10][10],X,Y,i,j,in_x,in_y,out_x,out_y;
double t1,t2;
struct maze *head,*p,*q;
printf("scale:n");
scanf("%d %d",&X,&Y);
printf("maze consisting of 0 (accessible) and 1 (inaccessible):n");
for(i=0;iY;i++)
{
for(j=0;jX;j++)
scanf("%d",&a[j]);
}
printf("in:n");
scanf("%d %d",&in_x,&in_y);
printf("out:n");
scanf("%d %d",&out_x,&out_y);
t1=clock();
head=(struct maze *)malloc(sizeof(struct maze));
head-prior=NULL;
p=head;
q=p;
p-x=in_x-1;
p-y=in_y-1;
p-dir=1;
loop:
do
{
q=(struct maze *)malloc(sizeof(struct maze));
p-next=q;
q-prior=p;
q-dir=1;
switch(p-dir)
{
case 1:q-x=p-x+1;q-y=p-y;break;
case 2:q-x=p-x;q-y=p-y+1;break;
case 3:q-x=p-x-1;q-y=p-y;break;
case 4:q-x=p-x;q-y=p-y-1;break;
default:
free(q);
q=p-prior;
free(p);
p=q;
if(p==NULL)
{
printf("No way out!n");
break;
}
else
{
p-dir+=1;
goto loop;
}
}
if(p==NULL)
break;
if(q-x0||q-x(X-1)||q-y0||q-y(Y-1))
{
free(q);
q=p;
p-dir+=1;
}
else
{
if(a[q-y][q-x]==1)
{
free(q);
q=p;
p-dir+=1;
}
else
{
if(cmp(head,q)==0)
{
free(q);
q=p;
p-dir+=1;
}
else
p=q;
}
}
}
while((p-x!=(out_x-1))||(p-y!=(out_y-1)));
if(p!=NULL)
{
p-next=NULL;
p=head;
while(p)
{
printf("(%d,%d)n",p-x+1,p-y+1);
p=p-next;
}
}
t2=clock();
printf("Time used is %lf.n",(t2-t1)/1000);
}
学了栈,做了一个迷宫程序。
楼主 2015-12-10 14:24 回复
Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
意见反馈 |
关于直线 |
版权声明 |
会员须知