#include<stdio.h>
#include<conio.h>
int main()
{
int k1,k2; //position of king
int h1,h2; //position of horse
int mov;
printf("Enter the position of king\n\tEnter the row =>");
scanf("%d",&k1);
printf("Enter the column => ");
scanf("%d",&k2);
printf("Enter the position of horse\n\tEnter the row => ");
scanf("%d",&h1);
printf("Enter the column");
scanf("%d",&h2);
mov=fun(k1,k2,h1,h2);
if(mov>9000)
printf("\n\n\tN0 possible move");
else
printf("\n\n\tminimun no of move => %d",mov);
}
int fun(int k1,int k2,int g1,int g2)
{
int a,b;
if(g1<1 || g2<1 || g1>8 || g2>8)
return 10000;
if(k1==g1 && k2==g2)
return 0;
if((k1+1==g1 && (k2+1==g2 || k2-1==g2)) || (k1-1==g1 && (k2-1==g2 || k2+1==g2)))
return 2;
if((k1==g1 &&(k2+1==g2 || k2-1==g2)) ||(k2==g2 && (k1+1==g1 || k1-1==g1)))
return 3;
else
{
if(k1<g1)
{
if(k2<g2)
{
a=fun(k1,k2,g1-1,g2-2);
b=fun(k1,k2,g1-2,g2-1);
}
else
{
a=fun(k1,k2,g1-2,g2+1);
b=fun(k1,k2,g1-1,g2+2);
if(k2>g2);
else
{
if(a>9000 || b>9000)
{
a=fun(k1,k2,g1-1,g2-2);
b=fun(k1,k2,g1-2,g2-1);
}
}
}
}
else
{
if(k2<g2)
{
a=fun(k1,k2,g1+1,g2-2);
b=fun(k1,k2,g1+2,g2-1);
}
else
{
a=fun(k1,k2,g1+2,g2+1);
b=fun(k1,k2,g1+1,g2+2);
if(k2>g2);
else
{
if(a>9000 || b>9000)
{
a=fun(k1,k2,g1+1,g2-2);
b=fun(k1,k2,g1+2,g2-1);
}
}
}
}
return((a>b)?b+1:a+1);
}
}