题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<queue>
using namespace std;
const int maxn=100000;
int dx[8]={-2,-2,-1,1,2,2,1,-1};
int dy[8]={-1,1,2,2,1,-1,-2,-2};
bool vis[10][10];
int ex,ey;
struct node{
int x,y;
int k;
}rt,ne;
int BFS(){
queue<node>M;
M.push(ne);
memset(vis,false,sizeof(vis));
vis[ne.x][ne.y]=true;
while(!M.empty()){
rt=M.front(); M.pop();
if(rt.x==ex&&rt.y==ey)break;
for(int i=0;i<8;++i){
ne=rt;
ne.x+=dx[i], ne.y+=dy[i], ++ne.k;
if(ne.x<1||ne.y<1||ne.x>8||ne.y>8||vis[ne.x][ne.y])continue;
M.push(ne);
vis[ne.x][ne.y]=true;
}
}
return rt.k;
}
int main(){
char a,b;
int x,y;
while(~scanf("%c%d %c%d",&a,&x,&b,&y)){
getchar();
ex=b-'a'+1, ey=y;
ne.x=a-'a'+1, ne.y=x, ne.k=0;
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,x,b,y,BFS());
}
return 0;
}