A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

Input Specification

The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

Output Specification

For each test case, print one line saying "To get from  xx  to  yy  takes  n  knight moves.".

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.

题意:给定象棋(对这个题来说国际中国一样)棋盘上的两个坐标,问你从第一个点走到第二个点最少需要多少步。

刚刚学了一个bfs模板题,恰巧来了一个适合我去用的地方,这个题真心不错。

模板题我找的代码是求路径的,我就想,那就直接用回溯求一个方法数不就得了?

按照脑子里还不熟的代码搞了半天终于整出来了……

注意:多组数据的时候,一定清空队列!否则你会对于第二组输入的结果一脸懵逼……

还要sum是每次都要从0求和的!所以别忘了等于0!

底层实现的队列,看着代码挺整齐的就学来了

还要注意不能把两个点看成小矩形的两个顶点,因为肯定会走到外面去!

给出代码供参考:

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int vis[10][10];

char a,b;
int m,n;
int sum=0;

struct que
{
    int x;
    int y;
    int pre;
}q[300];

int next[16][2]={-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};

void cnt(int i)
{
    if(q[i].pre!=-1)
    {
        cnt(q[i].pre);
        sum++;
    }
}
void bfs(int x,int y)
{
    if(a==b&&m==n)
    {
        sum=-1;
        return ;
    }
    int front = 0,rear = 1;
    q[front].x=x;
    q[front].y=y;
    q[front].pre=-1;
    while(front < rear)
    {
        for(int i=0;i<8;i++)
        {
            int nx=next[i][0]+q[front].x;
            int ny=next[i][1]+q[front].y;
            if(nx<1||ny<1||nx>8||ny>8||vis[nx][ny])
                continue;
            else
            {
                vis[nx][ny]=1;
                q[rear].x=nx;
                q[rear].y=ny;
                q[rear].pre=front;
                rear++;
            }
            if(nx==b-'a'+1&&ny==n)cnt(front);
        }
        front++;
    }
}
int main()
{

    while(~scanf("%c%d %c%d",&a,&m,&b,&n))
    {
        getchar();
        memset(vis,0,sizeof(vis));
        bfs(a-'a'+1,m);
        printf("To get from %c%d to %c%d takes %d knight moves.\n",a,m,b,n,sum+1);
        sum=0;
    }
    return 0;
}