Knight Moves

来源:互联网 发布:python 远程ssh 编辑:程序博客网 时间:2024/06/08 18:22

Knight Moves

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 20 Accepted Submission(s) : 19
Problem Description

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

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

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

Sample Input

e2 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 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.

析:就是象棋中的“马”走“日“字形格,判断从一个点到另一个点的最少步数;
简单搜索,运用广度优先搜索;

代码:

#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;struct node{    int x;    int y;    int step;};int dx[8] = {1, 2, 2, 1,-1,-2,-2,-1};int dy[8] = {2, 1, -1, -2,-2,-1,1,2};int qx,qy;int vis[10][10];int bfs(int x,int y){    node s, now, next;                                //s为第一个访问的节点    s.x = x;    s.y = y;    s.step = 0;    vis[x][y] = 1;                                   //对访问过的点进行标记    queue<node>qu;    qu.push(s);                                      //第一个点入队    while(!qu.empty())    {        now = qu.front();                            //把正在访问的点提出来        qu.pop();                                    //提出来后进行出队操作,把他相邻符合条件的点全部入队                                                     //找到终点        if(now.x == qx && now.y == qy)            return now.step;        for(int i = 0; i < 8; i++)        {            next.x = now.x + dx[i];            next.y = now.y + dy[i];            next.step = now.step;            if(vis[next.x][next.y] == 0&&next.x>=1&&next.y>=1&&next.x<=8&&next.y<=8)            {                vis[next.x][next.y] = 1;                next.step += 1;                qu.push(next);            }        }    }}int main(){    char str1[4],str2[4];    int px,py,sum;    while(~scanf("%s %s",str1,str2))    {        memset(vis, 0, sizeof(vis));        px=str1[0]-'a'+1;        qx=str2[0]-'a'+1;        py=str1[1]-'0';        qy=str2[1]-'0';        sum=bfs(px,py);        printf("To get from %s to %s takes %d knight moves.\n",str1,str2,sum);    }    return 0;}