该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
编译通过了 但是运行不下去 提示q->a〔i〕出错 估计是指针出错了 但是找不到怎么出错了 求大神们帮帮忙 谢谢了
#include
#include
#defineN8
typedefintelemtype;
intboard[N][N];
typedefstruct
{
intx;
inty;
}postype;
typedefstructdate
{
postypeseat;
inta[8];
intb[8];
intflag;
structdate*next;
}stacktype;
stacktype*init(stacktype*top)
{
intm,n;
top=(stacktype*)malloc(sizeof(stacktype));
top->next=NULL;
for(m=0;m<8;m++)
for(n=0;n<8;n++)
board[m][n]=0;
returntop;
}
stacktype*push(stacktype*top,intxi,intyj)//进栈操作
{
stacktype*p;
if((p=(stacktype*)malloc(sizeof(stacktype)))==NULL)
return0;
p->seat.x=xi;
p->seat.y=yj;
p->flag=0;
p->next=top->next;
top->next=p;
returntop;
}
postypepop(stacktype*top)//出栈操作
{
stacktype*p;
postypex;
postypey;
y.x=-1;
y.y=-1;
if(top->next==NULL)
returny;
p=top->next;
top->next=p->next;
x=p->seat;
free(p);
returnx;
}
intpanduan(intx,inty)//判断位置是否合法
{
if(board[x][y]==0&&x>=0&&x<8&&y>=0&&y<8)
return1;
return0;
}
stacktype*nextpos(stacktype*top,intdirector)//点所要走的八个方向
{
stacktype*p;
stacktype*q;
q=(stacktype*)malloc(sizeof(stacktype));
p=top->next;
if(p==NULL)
returnNULL;
switch(director)
{
case0:q->seat.x=p->seat.x+1;q->seat.y=p->seat.y-2;break;
case1:q->seat.x=p->seat.x+2;q->seat.y=p->seat.y-1;break;
case2:q->seat.x=p->seat.x+2;q->seat.y=p->seat.y+1;break;
case3:q->seat.x=p->seat.x+1;q->seat.y=p->seat.y+2;break;
case4:q->seat.x=p->seat.x-1;q->seat.y=p->seat.y+2;break;
case5:q->seat.x=p->seat.x-2;q->seat.y=p->seat.y+1;break;
case6:q->seat.x=p->seat.x-2;q->seat.y=p->seat.y-1;break;
case7:q->seat.x=p->seat.x-1;q->seat.y=p->seat.y-2;break;
}
returnq;
}
voidsetquan(stacktype*top)//求权值
{
stacktype*p,*q;
intm;
inti,j;
q=top->next;
for(i=0;i<8;i++)
q->a[i]=8;
for(i=0;i<8;i++)
{
m=0;
for(j=0;j<8;j++)
{
p=nextpos(top,j);
if(panduan(p->seat.x,p->seat.y))
q->a[i]=m++;
}
}
}
voidquanpaixu(stacktype*top)//对权值进行排序并放到新的数组b中
{
stacktype*p=top->next;
inti,j;
intmin;
for(i=0;i<8;i++)
{
min=9;
for(j=0;j<8;j++)
{
if(p->a[j]
{
min=p->a[j];
p->b[i]=j;
}
}
}
}
stacktype*trypath(stacktype*top)//马尝试走函数
{
intm,i=0,j;
intbiaoji=0;
stacktype*p;;
stacktype*q;
q=(stacktype*)malloc(sizeof(stacktype));
while(i<63)
{
p=top->next;
setquan(top);
quanpaixu(top);
for(j=0;j<8;j++)
{
m=p->b[p->flag];
q=nextpos(top,m);//马往权值最小的方向走
if(panduan(q->seat.x,q->seat.y))
{
push(top,q->seat.x,q->seat.y);
board[q->seat.x][q->seat.y]=1;
biaoji=1;
i++;
break;
}
else
p->flag++;
}
if(biaoji==0)
{
board[q->seat.x][q->seat.y]=0;
pop(top);
i--;
}
}
returntop;
}
voidinput(stacktype*top)//输出函数
{
postypedian;
inti,j;
intm=0;
intsum=0;
while(top->next!=NULL)
{
dian=pop(top);
sum++;
board[dian.x][dian.y]=sum;
}
for(i=0;i<8;i++)
for(j=0;j<8;j++)
{
printf("%d",board[i][j]);
m++;
if(m%8==0)
printf("\n");
}
}
intmain()//主函数
{
stacktype*top;
intx,y;
top=init(top);
printf("请输入x,y(起始位置x>=0&&x<8&&y>=0&&y<8)\n");
scanf("%d%d",&x,&y);
top=push(top,x,y);
top=trypath(top);
input(top);
return0;
}