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

    Knight Moves

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

    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

    题意:骑士的移动。马的移动方式,8种方式,不解释了。 map保存,这种方法还真是管用,orz~

    不是亮点的亮点:

    {   1.发现,我是用栈来保存位置的,不是队列。额额...也可以   只是在出栈时注意就可以了...  

        2.寻找到后,continue;这个的启发 是线段树的reutrn;到深搜的return;到广搜的continue;    

         其实是可以的,在找到后,就没有必要在当前位置 再向 其他方向 搜了。

    }

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define HH 1111111
    int time[8][8],min;
    int map[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
    int zhan[30000],f;
    void bfs(int x1,int y1,int wzx,int wzy)
    {
        int i,x,y;
        zhan[++f]=x1;
        zhan[++f]=y1;
        time[x1][y1]=0;
        while(f>0)
        {
            y1=zhan[f--];
            x1=zhan[f--];
            for(i=0;i<8;i++)
            {
                //printf("ss\n");
                x=x1+map[i][0];
                y=y1+map[i][1];
                if(x>=0&&x<8 && y>=0&&y<8)
                {
                    if(x==wzx && wzy==y)
                    {
                        if(time[x1][y1]+1<min)
                            min=time[x1][y1]+1;
                        /continue;
                    }
                    else if(time[x1][y1]+1<time[x][y])
                    {
                    //    printf("%d    %d\n",x,y);
                        time[x][y]=time[x1][y1]+1;
                        zhan[++f]=x;
                        zhan[++f]=y;
                    }
                }
            }
        }
    }
    int main()
    {
        int x1,y1,x2,y2,i,j;
        char n[5],m[5];
        while(scanf("%s%s",n,m)>0)
        {
            x1=n[0]-'a';
            y1=n[1]-'1';
            x2=m[0]-'a';
            y2=m[1]-'1';
            for(i=0;i<8;i++)
                for(j=0;j<8;j++)
                    time[i][j]=HH;
            min=HH;
            f=0;
            if(x1==x2&&y1==y2)
                min=0;
            else
            {
                bfs(x1,y1,x2,y2);
            }
            printf("To get from %s to %s takes %d knight moves.\n",n,m,min);
        }
        return 0;
    }
  • 相关阅读:
    时域和频域
    Python实用笔记 (26)面向对象高级编程——定制类
    Python实用笔记 (25)面向对象高级编程——多重继承
    去重-pd.duplicated
    合并pd.merge
    拼接
    pandas读取txt、excel、csv
    pandas字符串操作
    常用数据编辑-相加对齐
    成员判断
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3076326.html
Copyright © 2011-2022 走看看