/*
算法竞赛入门经典177页,bfs,裸题
本题要点:
1、马走日,有8个方向,写好8 个方向, for(int i = 0; i < 8; ++i)
2、套用 bfs 的模型即可。
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MaxN = 9;
bool vis[MaxN][MaxN];
int dx[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
int dy[8] = {2, 1, -1, -2, -2, -1, 1, 2};
char s1[3], s2[3];
struct node
{
int x, y, cnt;
};
node s, t;
void bfs()
{
memset(vis, false, sizeof vis);
vis[s.x][s.y] = true;
queue<node> q;
q.push(s);
node a, b;
while(q.size())
{
a = q.front(); q.pop();
if(a.x == t.x && a.y == t.y)
{
printf("To get from %s to %s takes %d knight moves.\n", s1, s2, a.cnt);
return;
}
for(int i = 0; i < 8; ++i)
{
b.x = a.x + dx[i], b.y = a.y + dy[i], b.cnt = a.cnt + 1;
if(b.x >= 0 && b.x < 8 && b.y >= 0 && b.y < 8 && vis[b.x][b.y] == false)
{
vis[b.x][b.y] = true;
q.push(b);
}
}
}
}
int main()
{
while(scanf("%s%s", s1, s2) != EOF)
{
s.x = s1[0] - 'a', s.y = s1[1] - '1', s.cnt = 0;
t.x = s2[0] - 'a', t.y = s2[1] - '1';
bfs();
}
return 0;
}
/*
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
*/
/*
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.
*/