Knight Moves (BFS模板)

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 visitseach 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 solvesthe "difficult" part.

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

Input Specification

The input file will contain one or more test cases. Each test case consists of one linecontaining 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.

Source: University of Ulm Local Contest 1996
Submit    Status


题解:这是一道BFS的经典题,注意国际象棋的骑士(马)的走法是走任意3*2格子的对角线,所以说有八个方向,可以用一个数组记录这八个方向;还有注意输出格式上的问题,’\n’不要忘记;


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn =1e6;
const ll inf=0x3f3f3f3f;
int sx,sy,ex,ey;
int dir[8][2]={1,2,-1,2,-1,-2,1,-2,2,1,-2,1,-2,-1,2,-1};
int vis[10][10];
struct node
{
    int x;
    int y;
    int step;
};
int BFS()
{
    memset(vis,0,sizeof(vis));

    node start,head,t;

    start.x=sx;
    start.y=sy;
    start.step=0;
    vis[start.x][start.y]=1;
    queue<node> Q;
    Q.push(start);
    while(!Q.empty())
    {
        head=Q.front();
        Q.pop();
        if(head.x==ex && head.y==ey)
            return head.step;
        for(int i=0;i<8;i++)
        {
            int xx=head.x+dir[i][0];
            int yy=head.y+dir[i][1];
            if(xx>=1 && xx<=8 && yy>=1 && yy<=8 && !vis[xx][yy])
            {
                vis[xx][yy]=1;
                t.x=xx;
                t.y=yy;
                t.step=head.step+1;
                Q.push(t);
            }

        }
    }


    return 0;
}
int main()
{
     string s1,s2;
     while(cin>>s1>>s2)
     {
         sx=s1[0]-'a'+1;
         sy=s1[1]-'1'+1;
         ex=s2[0]-'a'+1;
         ey=s2[1]-'1'+1;
         cout<<"To get from "<<s1<<" to "<<s2<<" takes "<<BFS()<<" knight moves."<<endl;

     }


    return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_41021816/article/details/80627480