I have to create a program in C that will be checking for a solution of Knight problem (in chess) for every possible starting point in the chess board. Sadly after writing everything down it won't compile and after searching for a long time, I wasnt able to find any solution.
#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;
}