#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef struct Node
{
int x, y, stp;
}Node;
int vid[9][9];
int dir[8][2] = {1, -2, -1, -2,
-2, -1, -2, 1,
-1, 2, 1, 2,
2, -1, 2, 1};
int ans, si, sj, ei, ej;
int check(int x, int y)
{
if (x>0 && x<=8 && y>0 && y<=8)
return 1;
return 0;
}
int main()
{
char c1[3], c2[3];
while (cin>>c1)
{
sj = c1[0]-'a'+1;
si = c1[1]-'0';
cin>>c2;
ej = c2[0]-'a'+1;
ei = c2[1]-'0';
memset(vid, 0, sizeof(vid));
queue<Node> que;
Node p;
p.x = si, p.y = sj, p.stp = 0;
que.push(p);
vid[si][sj] = 1;
while (!que.empty())
{
p = que.front();
que.pop();
if (p.x==ei && p.y==ej)
{
ans = p.stp;
break;
}
for (int d = 0; d < 8; d++)
{
int cx = p.x+dir[d][0], cy = p.y+dir[d][1];
if (check(cx, cy) && !vid[cx][cy])
{
Node next;
next.x = cx, next.y = cy, next.stp = p.stp+1;
que.push(next);
}
}
}
cout<<"To get from "<<c1<<" to "<<c2<<" takes "<<ans<<" knight moves."<<endl;
}
}
hdu 1372 Knight Moves
最新推荐文章于 2019-08-11 15:04:19 发布