/*vector_change ---HDU1372*/
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
/*问题目标仅仅只是要知道是否存在一条路径从fx,fy到达lx,ly*/
int dir[8][2] ={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; //先纵后横
//knight走的方向
struct node{
int x, y;
int num; //表示knight走的次数
};
int vis[10][10]; //表示访问
int fx, fy, lx, ly;
char a[5], b[5];
void bfs(int x, int y)
{
//initial
node p, t;
p.x = x; p.y = y; p.num = 0;
memset(vis, 0, sizeof(vis)); //初始化
vis[p.x][p.y] = 1; //表示访问过
queue<node> d;
d.push(p);
//initial end
while( !d.empty() )
{
t = d.front();
d.pop();
if( t.x == lx && t.y==ly ){
printf("To get from %s to %s takes %d knight moves.\n", a, b, t.num);
return ;
}
for( int i=0; i<8; i++) //遍历八个方向
{
if( t.x+dir[i][0]>0 && t.y+dir[i][1]>0 && t.x+dir[i][0]<9 && t.y+dir[i][1]<9 && (!vis[t.x+dir[i][0]][t.y+dir[i][1]]) )
{
p.x = t.x+dir[i][0];
p.y = t.y+dir[i][1];
p.num = t.num + 1;
d.push(p);
}
}
}
}
//15-2-5
int main()
{
//freopen("1.txt", "r", stdin);
while( ~scanf("%s%s", a, b) )
{
fx = a[0] - 'a' + 1; fy = a[1] - '0';
lx = b[0] - 'a' + 1; ly = b[1] - '0';
bfs(fx, fy);
}
return 0;
}
HDU1372
最新推荐文章于 2020-02-25 19:08:43 发布