题目链接
这题和石油那题特别像
按照象棋中马走日的方法从一个点到另外一个点 求最短的步数
比较水的题~~~~~
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int go[8][2] = {{-2,-1},{-2,1},{-1,-2},{1,-2},{-1,2},{1,2},{2,1},{2,-1}};
char n, N;
int m, M, sx, sy, ey, ex, sum = 0;
int vis[10][10],ans;
struct point
{
int x,y;
int step;
}p;
void bfs(int x1,int y1,int x2,int y2)
{
p.x = x1,p.y = y1,p.step = 0;
queue<point>q;
q.push(p);
point current, next;
vis[x1][y1] = 1;
while(!q.empty())
{
current = q.front();
q.pop();
for(int i = 0; i < 8; i++)
{
next.x = current.x + go[i][0];
next.y = current.y + go[i][1];
next.step = current.step + 1;
if(next.x>=1&&next.x<=8&&next.y>=1&&next.y<=8&&vis[next.x][next.y]==0)
{
if(next.x==x2&&next.y==y2)
{
sum = current.step + 1;
return ;
}
vis[next.x][next.y] = 1;
q.push(next);
}
}
}
}
int main()
{
while(cin >> n >> m >> N >> M)
{
sum = 0;
memset(vis,0,sizeof(vis));
sx = n - 96,sy = m;
ex = N - 96,ey = M;
bfs(sx,sy,ex,ey);
printf("To get from %c%d to %c%d takes %d knight moves.\n",n,m,N,M,sum);
}
return 0;
}