#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=8;
const int most=15;
int chess[maxn][maxn];
int a1,a2,b1,b2;
int best;
char s1[5],s2[5];
int input(){
if(!(cin>>s1)) return 0;
cin>>s2;
a1=s1[0]-97;
a2=s1[1]-49;
b1=s2[0]-97;
b2=s2[1]-49;
return 1;
}
bool find_best(int a1, int a2,int sum){
if(a1==b1&&a2==b2&&sum<best) best=sum;
if(a1==b1&&a2==b2) return 0;
if(sum>=most) return 0;
sum++;
(a1-2>=0)&&(a2-1>=0)&&(sum<best)&&find_best(a1-2,a2-1,sum);
(a1-2>=0)&&(a2+1<maxn)&&(sum<best)&&find_best(a1-2,a2+1,sum);
(a1-1>=0)&&(a2-2>=0)&&(sum<best)&&find_best(a1-1,a2-2,sum);
(a1-1>=0)&&(a2+2<maxn)&&(sum<best)&&find_best(a1-1,a2+2,sum);
(a1+1<maxn)&&(a2-2>=0)&&(sum<best)&&find_best(a1+1,a2-2,sum);
(a1+1<maxn)&&(a2+2<maxn)&&(sum<best)&&find_best(a1+1,a2+2,sum);
(a1+2<maxn)&&(a2-1>=0)&&(sum<best)&&find_best(a1+2,a2-1,sum);
(a1+2<maxn)&&(a2+1<maxn)&&(sum<best)&&find_best(a1+2,a2+1,sum);
}
int main(){
while(input()){
int sum=0;
best=30;
find_best(a1 ,a2 ,sum);
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,best);
}
return 0;
}
UVA439 - Knight Moves
最新推荐文章于 2021-08-01 10:07:45 发布