给出起点和终点 求骑士从起点走到终点所需要的步数
Sample Input
e2 e4 //起点 终点
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std; int map[][];
int sx, sy;
int ex, ey;
bool flag;
char s1[] , s2[] ; int dx[] = {-,-,,,-,-,,} ;
int dy[] = {,,,,-,-,-,-} ; struct node
{
int x , y , step ;
}; int bfs()
{
queue<node> q ;
int i , fx ,fy ;
node now , t ;
now.x = sx ;
now.y = sy ;
now.step = ;
q.push(now) ;
memset(map , , sizeof(map)) ;
map[sx][sy] = ;
while(!q.empty())
{
now = q.front() ;
q.pop() ;
for (i = ; i < ; i++)
{
fx = now.x + dx[i] ;
fy = now.y + dy[i] ;
if (fx< || fy< || fx>= || fy>= || map[fx][fy] == )
continue ;
if (fx == ex && fy == ey)
{
return now.step+ ;
}
t.x = fx ;
t.y = fy ;
t.step = now.step+ ;
q.push(t) ;
map[fx][fy] = ;
}
}
return ;
} int main()
{
//freopen("in.txt","r",stdin) ;
while (scanf("%s %s" , &s1 , &s2) !=EOF)
{
sx = s1[] - 'a' ;
sy = s1[] - '' ;
ex = s2[] - 'a' ;
ey = s2[] - '' ;
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,bfs()) ;
} return ;
}