zoukankan      html  css  js  c++  java
  • HDU1372 bfs

    Knight Moves

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


    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
     
    输入起点与终点,
    输出起点到终点的最少步数
    每个节点有八个方向,如图
    最少步数,显然直接bfs就好了
    这是一个二维的bfs,国际象棋(呃。还是要了解一下的)中骑士的走法是“目”,与中国象棋中的马的走法相似。

     注意输出格式。。。

    上代码:

    #include<iostream>
    #include<queue>
    #include<cstdio>
    using namespace std;
    ///二维空间  bfs搜索
    
    struct node
    {
        char x;
        int y;
    }s,e;
    queue<node> q;
    int step[1000][1000];
    ///这里开的小的话,会RE,还不知道为什么,有知道的大佬麻烦告诉一下我,谢谢了~~
    
    void bfs()
    {
        int xx[10]={1,2,2,1,-1,-2,-2,-1};
        int yy[10]={2,1,-1,-2,-2,-1,1,2};
        node m,n;///现在和下一步的节点
        while(!q.empty()){
            m = q.front();
            q.pop();
            for(int i = 0 ; i < 8 ; i++){
                n.x = m.x + xx[i];
                n.y = m.y + yy[i];
                if(n.x >= 'a' && n.x <= 'h' && n.y >= 1 && n.y <= 8 && step[n.x][n.y] == -1){
                    step[n.x][n.y] = step[m.x][m.y] + 1;
                    q.push(n);
                }
            }
        }
    }
    int main()
    {
        while(cin >> s.x >> s.y >> e.x >> e.y){
            for(int i = 'a' ; i <= 'h' ; i++){
                for(int j = 1 ; j <= 8 ; j++){
                    step[i][j] = -1;
                }
            }
            step[s.x][s.y] = 0;
            q.push(s);
            bfs();
            printf("To get from %c%d to %c%d takes %d knight moves.
    ",s.x,s.y,e.x,e.y,step[e.x][e.y]);
        }
        return 0;
    }

    有问题还请大家多多指教~

  • 相关阅读:
    Oracle 10g 改机器名后监听不能启动 解决方案 TNS12541 TNS12545
    14.3.1 IMPDP 命令行选项
    14.3.2 调用IMPDP —— 导入表空间
    三十岁之前不必在乎的事
    正则表达式
    GridLayout and GridData
    非UI线程更新界面
    整理用Java实现数字转化成字符串左边自动补零方法
    stackLayout
    读写properties文件
  • 原文地址:https://www.cnblogs.com/gudygudy/p/7215437.html
Copyright © 2011-2022 走看看