#include<stdio.h>
#include<time.h>
void print_chess();
int House(int x, int y, int step);
int chess_table[8][8];
int next_step(int* x,int* y,int count)
{
switch(count)
{
case 0:
if(*x+2<8 && *y+1<8 && chess_table[*x+2][*y+1]==0){
*x+=2;*y+=1;
return 1;
}
break;
case 1:
if(*x+1<8 && *y+2<8 && chess_table[*x+1][*y+2]==0){
*x+=1;*y+=2;
return 1;
}
break;
case 2:
if(*x+2<8 && *y-1>0 && chess_table[*x+2][*y-1]==0){
*x+=2;*y-=1;
return 1;
}
break;
case 3:
if(*x+1<8 && *y-2>0 && chess_table[*x+1][*y-2]==0){
*x+=1;*y-=2;
return 1;
}
break;
case 4:
if(*x-1>0 && *y-2>0 && chess_table[*x-1][*y-2]==0){
*x-=1;*y=2;
return 1;
}
break;
case 5:
if(*x-2>0 && *y-1>0 && chess_table[*x-2][*y-1]==0){
*x-=2;*y-=1;
return 1;
}
break;
case 6:
if(*x-2>0 && *y+1<8 && chess_table[*x-2][*y+1]==0){
*x-=2;*y+=1;
return 1;
}
break;
case 7:
if(*x-1>0 && *y+2<8 && chess_table[*x-1][*y+2]==0){
*x-=1;*y+=2;
return 1;
}
break;
default:
break;
}
return 0;
}
int House(int x,int y,int step)
{
int _x=x;
int _y=y;
int flag=0;
int count=0;
chess_table[x][y]=step;//默认在(x,y)为step步
if(step==64)//循环在step=25时退出
{
print_chess();//print_chess
return 1;
}
flag=next_step(&_x,&_y,count);//当step小于25说明存在下一部
while(0==flag && count<7) //用传地址的方法找下一步落点
{ //向八个方向查找
count ++;
flag=next_step(&_x,&_y,count);
}
while(flag)//找到当前新的值进入下一步
{
if(House(_x,_y,step+1))
{
return 1;
}//此处House如果=0及进入下一步后无路可走,则退一步,找当前步新的可能
_x=x;
_y=y;
count++;
flag=next_step(&_x,&_y,count);
while(flag==0 && count<7) //用传地址的方法找下一步落点
{ //向剩下方向查找
count ++;
flag=next_step(&_x,&_y,count);
}
}
if(0==flag)//flag仍为0,八方找不到下一步
{
chess_table[x][y]=0;//否认x,y为step部
}
return 0;
}
void print_chess()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
printf("%d\t",chess_table[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
int i,j;
clock_t start,finish;
start =clock();
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
chess_table[i][j]=0;
}
}
if(!House(2,0,1))
{
printf("lost\n");
}
finish=clock();
printf("spend %lf second:",(double)(finish-start)/CLOCKS_PER_SEC);
return 0;
}
请教,运行一小时无果,检查不出问题,请告知如何修改