现在的位置: 首页 > 综合 > 正文

HDU 1372Knight Moves(简单的BFS)

2013年11月04日 ⁄ 综合 ⁄ 共 2492字 ⁄ 字号 评论关闭

Knight Moves

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


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.
 
题目大意:题目意思很简单,从一个点到另一个点的最小步数,直接二维压成一维,BFS。用一个res保存步数,visi判断是否走过。

   题目地址:Knight Moves

AC代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
using namespace std;
char a[10];
struct node
{
    int x;
    int y;
};
node sta,en;  //sta为起点,en为终点
int visi[10][10];  //查看某点有无被访问过
int res[10][10];   //记录每个点的步数
int dir[8][2]=  //八个方向
{
    {-2,-1},{2,-1},{-2,1},{2,1},
    {-1,-2},{1,-2},{-1,2},{1,2}
};
queue <int>q;

void bfs()
{
    memset(visi,0,sizeof(visi));
    memset(res,100,sizeof(res));
    while(!q.empty())  //将队清空
        q.pop();
    int i;
    q.push(sta.x*8+sta.y);  //将起点入队
    visi[sta.x][sta.y]=1;
    res[sta.x][sta.y]=0;
    while(!q.empty())
    {
        int tmp=q.front();
        q.pop();
        int cx=tmp/8;  //再转换成坐标
        int cy=tmp%8;  //current的x,y
        if(cx==en.x&&cy==en.y)
            return;
        for(i=0;i<8;i++)
        {
            int px,py;
            px=cx+dir[i][0],py=cy+dir[i][1];
            if(!visi[px][py]&&px>=0&&px<8&&py>=0&&py<8)  //防止越界
            {
                res[px][py]=res[cx][cy]+1;  //比上次多一步
                visi[px][py]=1;
                q.push(px*8+py);
            }
        }
    }
}

int main()
{
    while(gets(a))
    {
        sta.x=a[0]-'a';   //转化为坐标
        sta.y=a[1]-'1';
        en.x=a[3]-'a';
        en.y=a[4]-'1';
        bfs();
        //cout<<sta.x<<" "<<sta.y<<endl;
        //cout<<en.x<<" "<<en.y<<endl;
        printf("To get from %c%c to %c%c takes %d knight moves.\n",a[0],a[1],a[3],a[4],res[en.x][en.y]);
    }
    return 0;
}

//31MS 240K
/*
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
*/

抱歉!评论已关闭.