zoukankan      html  css  js  c++  java
  • HDU 1372 Knight Moves

    Knight Moves

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

    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
     
    Recommend
    Eddy
     
    思路:虽然是一道打水BFS,但是不懂得国际象棋的大水牛就是坑爹啊,国际象棋的步伐果然是鸟人心碎啊
    哒哒哒
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    char
    s[10];
    int
    ax,ay,bx,by;
    int
    step;
    int
    hash[10][10];
    int
    bfs[8][2] =  { -2, 1, 2, 1, 2, -1, -2, -1, 1, 2, 1, -2, -1, 2, -1, -2 };
    struct
    Node
    {

        int
    x,y;
        int
    step;
    };

    int
    BFS()
    {

        queue <Node> q;
        Node top;
        top.x = ax;top.y = ay;
        //printf("%d %d ",ax,ay);
        top.step = 0;
        q.push(top);
        while
    (!q.empty())
        {

            Node temp;
            temp = q.front();
            q.pop();
            //printf("%d %d have %d ",temp.x,temp.y,temp.step);
            if(temp.x == bx && temp.y == by)
                       return
    temp.step;
            for
    (int i = 0;i < 8;i ++)
            {

                int
    x = temp.x + bfs[i][0];int y = temp.y + bfs[i][1];
                int
    step = temp.step + 1;
                //printf("%d %d ",x,y);
                if(hash[x][y] == 0 && x <= 8 && x >= 1 && y <= 8 && y >= 1)
                {

                    Node xin;
                    hash[x][y] = 1;
                    xin.x = x;xin.y = y;
                    xin.step = step;
                    q.push(xin);
                }
            }
        }
    }

    int
    main()
    {

        while
    (gets(s))
        {

            ax = s[0] - 'a' + 1;ay = s[1] - '0';
            bx = s[3] - 'a' + 1;by = s[4] - '0';
            //printf("%d %d %d %d ",ax,ay,bx,by);
            memset(hash,0,sizeof(hash));
            step = 0;
            hash[ax][ay] = 1;
            step = BFS();
            printf("To get from %c%c to %c%c takes %d knight moves. ",
               s[0],s[1],s[3],s[4],step);
        }
    }
  • 相关阅读:
    Oracle 实例恢复
    使用 ASMCMD 工具管理ASM目录及文件
    软考编译原理总结
    解决初学者学不懂android,不理解android的设计
    对计算机模拟人脑的一个小想法
    [每日一题] 11gOCP 1z0-052 :2013-09-19 创建用户...................................................B41
    andengine游戏引擎总结基础篇
    hdu 1789 Doing Homework again (贪心)
    poj 3026 (最小生成树)
    autolisp 列表 resbuf
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3283562.html
Copyright © 2011-2022 走看看