#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char ss[3],ee[3];
int x1,y1,x2,y2;
struct pos
{
int x,y,step;
}sta,end;
int f[10][10];
int dir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};
bool fan(int a,int b)
{
if(a>0 && a<=8 && b>=1 && b<=8 && f[a][b]==0)
return true;
return false;
}
int bfs(int a,int b)
{
int i;
queue<pos>q;
sta.x=a;
sta.y=b;
f[a][b]=1;
sta.step=0;
q.push(sta);
while(!q.empty())
{
memset(f,0,sizeof(f));
sta=q.front();
q.pop();
if(sta.x==x2 && sta.y==y2)
return sta.step;
for(i=0;i<8;i++)
{
end.x=sta.x+dir[i][0];
end.y=sta.y+dir[i][1];
if(fan(end.x,end.y))
{
end.step=sta.step+1;
f[end.x][end.y]=1;
q.push(end);
}
}
}
}
int main()
{
int i,j,k;
while(scanf("%s%s",ss,ee)!=EOF)
{
x1=ss[0]-'a'+1;
y1=ss[1]-'0';
x2=ee[0]-'a'+1;
y2=ee[1]-'0';
int as=bfs(x1,y1);
printf("To get from %s to %s takes %d knight moves.\n",ss,ee,as);
}
}
标签:hdu,y2,sta,--,Knight,int,step,&&,include
From: https://blog.51cto.com/u_16244339/7444183