#include<iostream>
#include<cstdio>
#include<cstring>
const int dx[8] = {-2,-2,-1,-1,1,1,2,2};
const int dy[8] = {-1,1,-2,2,-2,2,-1,1};
typedef struct
{
int x,y,st;
} point;
point q[10000];
char st[10],en[10];
int v[10][10];
using namespace std;
void solve(int xx,int yy,int xxx,int yyy)
{
int head=0,tail=1;
q[1].x=xx;
q[1].y=yy;
q[1].st=0;
v[xx][yy]=true;
while(head<=tail)
{
head++;
for(int i=0; i<8; i++)
{
int x=q[head].x+dx[i];
int y=q[head].y+dy[i];
//cout<<x<<" "<<y<<endl;
// cout<<x<<" "<<y<<endl;
if(!v[x][y]&&x>=1&&x<=8&&y>=1&&y<=8)
{
tail++;
q[tail].x=x;
q[tail].y=y;
q[tail].st=q[head].st+1;
v[x][y]=true;
}
if(x==xxx&&y==yyy)
{
printf("To get from %s to %s takes %d knight moves.\n",st,en,q[tail].st);
return;
}
}
}
}
int main()
{
while(scanf("%s%s",st,en)!=EOF)
{
int x,y,xx,yy;
memset(q,0,sizeof(q));
memset(v,0,sizeof(v));
x=st[1]-'0';
y=st[0]-'a'+1;
xx=en[1]-'0';
yy=en[0]-'a'+1;
// cout<<x<<" "<<y<<" "<<xx<<" "<<yy<<endl;
if(x==xx&&y==yy) printf("To get from %s to %s takes %d knight moves.\n",st,en,0);
else
solve(x,y,xx,yy);
}
return 0;
}
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=380