已结贴√
问题点数:15 回复次数:6
马踏棋盘问题
只能执行到第二步,找不出结果,求各位帮忙
#include
#include
#include
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct Point{
int x;
int y;
int e;
}Point;
typedef struct {
Point *base;
Point *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
(*S).base =(Point *)malloc(STACK_INIT_SIZE*sizeof(Point));
if(!(*S).base)exit(1);
(*S).top = (*S).base;
(*S).stacksize = STACK_INIT_SIZE;
return 1;
}
int StackEmpty(SqStack *S)
{
if((*S).top==(*S).base)
return 1;
else
return 0;
}
int Push(SqStack *S,Point e)
{
if((*S).top - (*S).base >= (*S).stacksize)
{
(*S).base =(Point *)realloc((*S).base,((*S).stacksize+STACKINCREMENT) * sizeof(Point));
if(!(*S).base)exit(1);
(*S).top = (*S).base+(*S).stacksize;
(*S).stacksize += STACKINCREMENT;
}
*(*S).top++ = e;
return 1;
}
int Pop(SqStack *S, Point *e)
{
if((*S).top ==(*S).base) return 0;
*e = * --(*S).top;
return 1;
}
int SetTop(SqStack S,Point *e)
{
if(S.top>S.base)
{
*(S.top-1)=*e;
return 1;
}
else
return 0;
}
void main()
{
Point e;
int count=1;
int i,j;
int board[8][8]={{0}};
int x1,y1;
int move1[8]={2,2,1,1,-1,-1,-2,-2,},
move2[8]={1,-1,2,-2,2,-2,1,-1,};
int pic[8][8]; //对应棋盘
SqStack S;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
pic[i][j]=0;
//输入入口点,并让其进栈
printf("输入x1(0-7),y1(0-7):");
scanf("%d%d",&x1,&y1);
e.x=x1;
e.y=y1;
pic[x1][y1]=1;
InitStack(&S);
Push(&S,e);
int k=0;
while(count!=64)
{
if(x1+move1[k]>0&&x1+move1[k]<=8&&y1+move2[k]>0&&y1+move2[k]<=8&&board[x1+move1[k]][y1+move2[k]]==0)
{
pic[x1+move1[k]][y1+move2[k]]=++count;
e.x=x1+move1[k];
e.y=y1+move2[k];
Push(&S,e);
k=0;
}
else
board[x1+move1[k]][y1+move2[k]]=1;
k++;
}
while(!StackEmpty(&S))
{
Pop(&S,&e);
printf("(%d %d)",e.x,e.y);
}
printf("\n");
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
printf("%d\t",pic[i][j]);
printf("\n\n\n");
}
}
搜索更多相关主题的帖子:
return