需要国际象棋的知识,棋盘是1-8,a-h,骑士就是马啦,走的是2*3的格子的对角线,那么就是有八个方位
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#define N 105
using namespace std;
struct node{
int x,y,step;
};
int n,m;
int vis[N][N],map[N][N];
int ex,ey;
char s1[10],s2[10];
int check(int x,int y){
if(x<=0||x>n||y<=0||y>m||map[x][y])return 1;
return 0;
}
int bfs(){
int i;
queue<node>Q;
node a,next;
a.x=s1[0]-'a'+1;
a.y=s1[1]-'0';
ex=s2[0]-'a'+1;
ey=s2[1]-'0';
//printf("%d %d\n",a.x,a.y);
a.step=0;
memset(map,0,sizeof(map));
map[a.x][a.y]=1;
Q.push(a);
while(!Q.empty()){
//printf("111\n");
a=Q.front();
Q.pop();
if(a.x==ex&&a.y==ey)return a.step;
next.x=a.x+1;
next.y=a.y+2;
if(next.x==ex&&next.y==ey)return a.step+1;
if(!check(next.x,next.y)){
next.step=a.step+1;
map[next.x][next.y]=1;
Q.push(next);
}
next.x=a.x+2;
next.y=a.y+1;
if(next.x==ex&&next.y==ey)return a.step+1;
if(!check(next.x,next.y)){
next.step=a.step+1;
map[next.x][next.y]=1;
Q.push(next);
}
next.x=a.x+2;
next.y=a.y-1;
if(next.x==ex&&next.y==ey)return a.step+1;
if(!check(next.x,next.y)){
next.step=a.step+1;
map[next.x][next.y]=1;
Q.push(next);
}
next.x=a.x+1;
next.y=a.y-2;
if(next.x==ex&&next.y==ey)return a.step+1;
if(!check(next.x,next.y)){
next.step=a.step+1;
map[next.x][next.y]=1;
Q.push(next);
}
next.x=a.x-1;
next.y=a.y+2;
if(next.x==ex&&next.y==ey)return a.step+1;
if(!check(next.x,next.y)){
next.step=a.step+1;
map[next.x][next.y]=1;
Q.push(next);
}
next.x=a.x-2;
next.y=a.y+1;
if(next.x==ex&&next.y==ey)return a.step+1;
if(!check(next.x,next.y)){
next.step=a.step+1;
map[next.x][next.y]=1;
Q.push(next);
}
next.x=a.x-2;
next.y=a.y-1;
if(next.x==ex&&next.y==ey)return a.step+1;
if(!check(next.x,next.y)){
next.step=a.step+1;
map[next.x][next.y]=1;
Q.push(next);
}
next.x=a.x-1;
next.y=a.y-2;
if(next.x==ex&&next.y==ey)return a.step+1;
if(!check(next.x,next.y)){
next.step=a.step+1;
map[next.x][next.y]=1;
Q.push(next);
}
}
return 0;
}
int main(){
n=8;m=8;
while(scanf("%s%s",s1,s2)!=EOF){
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,bfs());
}
}