【发布时间】:2015-11-21 17:49:59
【问题描述】:
我必须用 C 语言创建一个程序,该程序将检查棋盘中每个可能起点的奈特问题(在国际象棋中)的解决方案。可悲的是,在写下所有内容后它无法编译,并且在搜索了很长时间之后,我无法找到任何解决方案。
#include <stdio.h>
void print(int **ruchytab,int size);
void zewnetrzne(int size);
int knight(int **ruchytab,int x,int y,int ktory,int size);
int move(int **ruchytab,int x,int y,int wariant,int *newx,int *newy,int size);
int main()
{
int size=5;
zewnetrzne(size);
return 0;
}
void print(int **ruchytab,int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
printf("%2.d ",ruchytab[i][j]);
putchar('\n');
}
}
void zewnetrzne(int size)
{
int ruchytab[size][size];
for(int j=0;j<size;j++) //cleaning tab
{
for(int i=0;i<size;i++)
ruchytab[j][i]=0;
}
for(int a=0;a<size;a++) //diffrent start points
for(int b=0;b<size;b++)
{
knight(ruchytab,a,b,1,size);
for(int j=0;j<size;j++) //cleaning tab
{
for(int i=0;i<size;i++)
ruchytab[j][i]=0;
}
}
}
int knight(int **ruchytab,int x,int y,int ktory,int size)
{
int newx,newy;
ruchytab[x][y]=ktory;
if(ktory>=size*size) //we have only n^2 possible moves ,we have to be everywhere only once
{
print(ruchytab,size);
return 1;
}
else
{
for(int war=1;war<=8;war++)
if(move(ruchytab,x,y,war,&newx,&newy,size)==1)
if(knight(ruchytab,newx,newy,ktory+1,size)==1)
return 1;
}
return 0;
}
int move(int **ruchytab,int x,int y,int wariant,int *newx,int *newy,int size)
{
switch(wariant) //8 diffrent moves
{
case 1:
*newx=x-1;
*newy=y-2;
break;
case 2:
*newx=x+1;
*newy=y-2;
break;
case 3:
*newx=x+2;
*newy=y-1;
break;
case 4:
*newx=x+2;
*newy=y+1;
break;
case 5:
*newx=x+1;
*newy=y+2;
break;
case 6:
*newx=x-1;
*newy=y+2;
break;
case 7:
*newx=x-2;
*newy=y+1;
break;
case 8:
*newx=x-2;
*newy=y-1;
break;
}
if(*newx>=0 && *newx <size && *newy>=0 && *newy<size && ruchytab[*newx][*newy]==0) //checking if the move is possible and if the place was visited already
return 1;
else
return 0;
}
【问题讨论】:
-
错误信息是什么?