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

    题目链接

    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,但是和其他题目不同的是,这个走法是‘日’字形的,就像象棋中的马,还有就是这个棋盘没有障碍物,判断什么的就省了。

    #include <cstdio>
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #define PI acos(-1.0)
    #define ms(a) memset(a,0,sizeof(a))
    #define msp memset(mp,0,sizeof(mp))
    #define msv memset(vis,0,sizeof(vis))
    using namespace std;
    //#define LOCAL
    int spx,spy,epx,epy;
    int dir[8][2] = {{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};
    int mp[9][9];
    int vis[9][9];
    struct Node
    {
        int x,y;
        int time;
    };
    int bfs(int t)
    {
        msv;
        vis[spx][spy]=1;
        queue<Node> q;
        while(!q.empty())q.pop();
        Node node;
        node.time=0,node.x=spx,node.y=spy;
        q.push(node);
        while(!q.empty())
        {
            node=q.front();
            q.pop();
            if(node.x==epx&&node.y==epy)return node.time;
            if(node.x>=8||node.x<0||node.y>=8||node.y<0)continue;
            for(int i=0;i<8;i++)
            {
                Node nn;
                nn.time=node.time+1;
                nn.x=node.x+dir[i][0];
                nn.y=node.y+dir[i][1];
                if(vis[nn.x][nn.y])continue;
                else q.push(nn);
            }
        }
        return -1;
    }
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
    #endif // LOCAL
        ios::sync_with_stdio(false);
        char sp[5],ep[5];
        while(cin>>sp>>ep)
        {
            spx=sp[1]-'1',spy=sp[0]-'a';
            epx=ep[1]-'1',epy=ep[0]-'a';
            msp;
            int ans=bfs(0);
            printf("To get from %s to %s takes %d knight moves.
    ",sp,ep,ans);
        }
        return 0;
    }
  • 相关阅读:
    配置hive的元数据到Mysql中
    对部分spi控制的外设芯片需要断电再上电处理
    驱动芯片L9945的odd parity的计算方法
    bit field不具备可移植性
    multiple storage classes in declaration specifiers报错解决
    C编译器中对h文件的重重包含导致的预处理不同的问题
    linker file链接文件
    eclipse编辑器中的TODO和FIXME的使用
    解决hightec的链接文件的路径问题
    LIN开发
  • 原文地址:https://www.cnblogs.com/gpsx/p/5184706.html
Copyright © 2011-2022 走看看