HDU : Knight Moves

Knight Moves

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 30   Accepted Submission(s) : 24
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

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.
 
题意:
给定一个8*8的棋盘,给定两个棋子的坐标,求一个棋子相遇的最小步数,走的规则按“日”的规律走;
a--h对应了1--8;例:e2  e4代表(5,2)和(5,4)两个点;

解题思路:从已知一个点出发利用bfs遍历所有可能,直到找到另一个已知点为止。最先找到所用的步数即为最小步数。

源代码:


#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
int x1,x2;
int y1,y2;
char str1,str2;
struct dele{
    int x;
    int y;
    int num;
};
int bfs(dele xin)
{
    queue<dele>q;
    q.push(xin);
    while(!q.empty())//队列不为空
    {
        dele p,pp;
        p=q.front();
        q.pop();
        if(p.x==x2&&p.y==y2)//找到就跳出循环,这就是最短步数
        {
            return p.num;

        }
        if(p.x+1>0&&p.y+2>0&&p.x+1<9&&p.y+2<9)//共8种可能,用8个if当然也可以用for循环
        {
            pp.x=p.x+1;
            pp.y=p.y+2;
            pp.num=p.num+1;//步数加一
            q.push(pp);//棋子所走的位置让其进队
        }
        if(p.x-1>0&&p.y+2>0&&p.x-1<9&&p.y+2<9)
        {
            pp.x=p.x-1;
            pp.y=p.y+2;
            pp.num=p.num+1;
            q.push(pp);
        }
        if(p.x+2>0&&p.y+1>0&&p.x+2<9&&p.y+1<9)
        {
            pp.x=p.x+2;
            pp.y=p.y+1;
            pp.num=p.num+1;
            q.push(pp);
        }
        if(p.x-2>0&&p.y+1>0&&p.x-2<9&&p.y+1<9)
        {
            pp.x=p.x-2;
            pp.y=p.y+1;
            pp.num=p.num+1;
            q.push(pp);
        }
        if(p.x+2>0&&p.y-1>0&&p.x+2<9&&p.y-1<9)
        {
            pp.x=p.x+2;
            pp.y=p.y-1;
            pp.num=p.num+1;
            q.push(pp);
        }
        if(p.x+1>0&&p.y-2>0&&p.x+1<9&&p.y-2<9)
        {
            pp.x=p.x+1;
            pp.y=p.y-2;
            pp.num=p.num+1;
            q.push(pp);
        }
        if(p.x-2>0&&p.y-1>0&&p.x-2<9&&p.y-1<9)
        {
            pp.x=p.x-2;
            pp.y=p.y-1;
            pp.num=p.num+1;
            q.push(pp);
        }
        if(p.x-1>0&&p.y-2>0&&p.x-1<9&&p.y-2<9)
        {
            pp.x=p.x-1;
            pp.y=p.y-2;
            pp.num=p.num+1;
            q.push(pp);
        }
    }
}
int main()
{
    while(scanf("%c%d %c%d",&str1,&x1,&str2,&x2)!=EOF)
    {
        getchar();//防治上次输入对结果造成影响
        dele zh;
        y1=str1-96;//把字符变为数字便于问题的解决
        y2=str2-96;
        zh.x=x1;zh.y=y1;
        zh.num=0;
        int sum=bfs(zh);
        printf("To get from %c%d to %c%d takes %d knight moves.\n",str1,x1,str2,x2,sum);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值