HDU-1372-Knight Moves

来源:互联网 发布:淘宝头条 写作 编辑:程序博客网 时间:2024/05/17 08:22

E - Knight Moves
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 1372
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 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.

题意,给出起始点。终止点坐标,骑士按照国际象棋中马的走法行动,也就是中国象棋中马的行动方式,不过不存在绊马腿的情况。输出最少步数。
骑士的行走也就有了八个方向,按照这八个方向广度搜索即可。

代码

#include<stdio.h>#include<string.h>#include<string>#include<stack>#include<queue>#include<math.h>#include<limits.h>#include<iostream>#include<algorithm>#define SIZE 100001using namespace std;//国际象棋走法,广度优先搜索int visited[10][10];//已访问标记为1,未访问初始化为0int dir[8][2]= {{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}}; //八个搜索方向int star_x,star_y,end_x,end_y;//起始终止点坐标char s1[3],s2[3];//接收两组数据struct node{    int x;    int y;    int time;    friend bool operator<(node m,node n)    {        return m.time>n.time;//耗时少的优先级高    }};int judge(int x,int y)//传入坐标{    if(x>0&&x<=8&&y>0&&y<=8&&visited[x][y]==0)        return 1;    return 0;}void BFS(){    priority_queue<node>q;    node star,end;    star.x=star_x;//传入坐标    star.y=star_y;    star.time=0;//初始化时间    memset(visited,0,sizeof(visited));    visited[star.x][star.y]=1;//标记起点已访问    q.push(star);    while(!q.empty())    {        star=q.top();        q.pop();        for(int i=0; i<8; i++)        {            if(star.x==end_x&&star.y==end_y)            {                printf("To get from %s to %s takes %d knight moves.\n",s1,s2,star.time);                return;            }            end.x=star.x+dir[i][0];            end.y=star.y+dir[i][1];            if(judge(end.x,end.y))//坐标合法且未被访问过            {                visited[end.x][end.y]=1;                end.time=star.time+1;                q.push(end);            }        }    }}int main(){    while(scanf("%s %s",s1,s2)!=EOF)    {        star_x=s1[0]-'a'+1;        star_y=s1[1]-'0';        end_x=s2[0]-'a'+1;        end_y=s2[1]-'0';        BFS();    }}
0 0