#include <stdio.h>
#define index 5 //rom and column
#define X 2
#define Y 2
struct toado
{
int x;
int y;
};
toado b[index*index];
int v[]={-1,1,2,2 ,1 ,-1,-2,-2};
int h[]={2 ,2,1,-1,-2,-2,-1,1};
int dem=0;
int horseTrace(int a[index][index],int x,int y,int n)
{
if ((x<0) || (x>index-1) || (y<0) || (y>index-1) || (a[x][y]==1))
return 0;
toado temp={x,y}; //push
b[n-1]=temp;
a[x][y]=1;
if (n==(index*index))
{
int i;
printf("\n%i: ",++dem); //pop
for (i=0;i<(index*index);i++)
{
temp=b[i];
printf("%i-%i ",temp.x,temp.y);
}
a[x][y]=0;
return 1;
}
else
{
int i;
int flag=0;
int xTemp, yTemp;
for(i=0;i<8;i++)
{
xTemp=x+v[i];
yTemp=y+h[i];
if (horseTrace(a,xTemp,yTemp,n+1))
flag=1;
}
a[x][y]=0;
if (flag)
return 1;
else
return 0;
}
}
int main()
{
int a[index][index]={0};
if (!horseTrace(a,X,Y,1))
printf("Ko tim thay loi giai");
getchar();
getchar();
return 0;
}
khoa code tí :)) lủng củng quá