首页 > 其他 > 详细

HDOJ 1372 Knight Moves【BFS】

时间:2015-02-09 23:03:15      阅读:492      评论:0      收藏:0      [点我收藏+]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372


Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7482    Accepted Submission(s): 4481


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 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

题意: 骑士环游,给定起点和终点,求出最少步数。

题解:8*8的期盼,可以把各点之间的补数先BFS求出打表,再直接输出就可以。根据棋盘的对称性,可以避免一些重复计算。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define N 10
using namespace std;
int step[N][N][N][N],visit[N][N];
int dir[][2]={
    {1,2},{1,-2},{-1,2},{-1,-2},
    {2,1},{2,-1},{-2,1},{-2,-1}
};
struct Node{
    int x,y,s;
};
int bfs(int sx,int sy,int ex,int ey)
{
    queue<Node> q;
    Node head={sx,sy,0};
    q.push(head);
    memset(visit,-1,sizeof(visit));
    visit[sx][sy]=0;
    while(q.size()){
        Node f=q.front();
        q.pop();
        if(f.x==ex&&f.y==ey)return f.s;
        for(int i=0;i<8;i++){
            int dx=dir[i][0]+f.x,dy=dir[i][1]+f.y;
            if(dx>=0&&dx<8&&dy>=0&&dy<8&&visit[dx][dy]){
                visit[dx][dy]=0;
                Node t={dx,dy,f.s+1};
                q.push(t);
            }
        }
    }
}
int main()
{
    cin.sync_with_stdio(false);
    memset(step,-1,sizeof(step));
    for(int i=0;i<8;i++)
    for(int j=0;j<8;j++)
        for(int k=0;k<8;k++)
        for(int l=0;l<8;l++)
        if(step[i][j][k][l]!=-1)continue;
        else if(i==k&&j==l) step[i][j][k][l]=0;
        else {
            int temp=bfs(i,j,k,l);
            step[i][j][k][l]=step[k][l][i][j]=temp;
            step[j][i][l][k]=step[l][k][j][i]=temp;
        }
    char c1,c2; int x1,y2;
    while(cin>>c1>>x1>>c2>>y2){
    printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,x1,c2,y2,step[c1-'a'][x1-1][c2-'a'][y2-1]);
    }
    return 0;
}

【转载请注明出处】

作者: MummyDing

出处:http://blog.csdn.net/mummyding


HDOJ 1372 Knight Moves【BFS】

原文:http://blog.csdn.net/mummyding/article/details/43679277

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!