• Problem B: 最少步数


    Description
    A friend of you is doing research on theTraveling Knight Problem (TKP) where you are to find the shortest closed tourof knight moves that visits each square of a given set of n squares on achessboard exactly once. He thinks that the most difficult part of the problemis determining the smallest number of knight moves between two given squaresand 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 programthat solves the “difficult” part.

    Your job is to write a program that takes two squares a and b as input and thendetermines the number of knight moves on a shortest route from a to b.

    Input
    The input will contain one or more testcases. Each test case consists of one line containing two squares separated byone space. A square is a string consisting of a letter (a-h) representing thecolumn 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*8,a-h,1-8;后来看了一点别人的
    输入的控制,两个字符数组,a和b就可以啦,注意马走日

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    int dx[]={-2,-1,1,2,2,1,-1,-2};
    int dy[]={-1,-2,-2,-1,1,2,2,1};
    struct dot
    {
        int x,y;
        int time;
    };
    inline bool in(dot gx)
    {
        if(gx.x>=0&&gx.x<8&&gx.y>=0&&gx.y<8)
            return true;
        return false;
    }
    int main()
    {
        char a[10],b[10];
        int vis[8][8];
        while(scanf("%s%s",&a,&b)!=EOF)
        {
     
            dot gx;
            int x1=a[0]-'a';
            int y1=a[1]-'1';
            int x2=b[0]-'a';
            int y2=b[1]-'1';
            gx.x=x1;
            gx.y=y1;
            gx.time=0;
            queue<dot>q;
            while(!q.empty())
                q.pop();
            q.push(gx);
            memset(vis,0,sizeof(vis));
            int step=0;
            if(strcmp(a,b))
            {
            while(!q.empty())
            {
                dot tmp,next;
                tmp=q.front(),q.pop();
                for(int i=0;i<8;i++)
                {
                    next.x=tmp.x+dx[i];
                    next.y=tmp.y+dy[i];
                    next.time=tmp.time+1;
                    if(in(next)&&!vis[next.x][next.y])
                    {
                        vis[next.x][next.y]=1;
                        q.push(next);
                        if(next.x==x2&&next.y==y2)
                        {
                            step=next.time;
                            break;
                        }
                    }
                }
                if(step>0)
                    break;
            }
            }
            else
                step=0;
            printf("To get from ");
            cout<<a<<" "<<"to"<<" "<<b;
            printf(" takes %d knight moves.
    ",step);
     
        }
     
    }
  • 相关阅读:
    Skype 1.4 for Linux 掉丢掉更新
    Skype 1.4 for Linux 失掉更新
    DiffMerge:可视化的文件相比与兼并东西
    Qt 4.3 公布揭晓
    Netscape Navigator 9 Beta 1 颁布
    Qtpfsgui:HDR 图片处置责罚器材
    Akregator 运用评测
    PenguinTV 3.0
    Exchange Server 2003备份
    Windows 编程[13] 菜单与菜单资源(二)
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4236901.html
Copyright © 2020-2023  润新知