zoukankan      html  css  js  c++  java
  • poj2243

    Knight Moves
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13433   Accepted: 7518

    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 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

    题意:求点xx到点yy的最少步数
    bfs模板
     

    今天学弟问我bfs怎么敲,于是就敲了一遍模板题

    stl版

    #include<queue>
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int N=200;
    int n,m;
    int vist[N][N];
    int dx[8]={-2,-2,-1,-1,1,1,2,2};
    int dy[8]={-1,1,-2,2,-2,2,-1,1};
    struct node{
        int x,y,num;
        char c;
    }a,b;
    queue<node>q;
    void bfs(){
        vist[a.x][a.y]=1;
        while(!q.empty()){
            node temp=q.front();q.pop();
            if(temp.x==b.x&&temp.y==b.y){
                printf("To get from %c%d to %c%d takes %d knight moves.
    ",a.c,a.y,b.c,b.y,temp.num);
                return ;
            }
            for(int i=0;i<8;i++){
                int xx=temp.x+dx[i];
                int yy=temp.y+dy[i];
                if(xx<1||xx>8||yy<1||yy>8||vist[xx][yy]) continue;
                node next;
                next.x=xx;
                next.y=yy;
                next.num=temp.num+1;
                vist[xx][yy]=1;
                q.push(next);
            }
        }
    }
    int main(){
        while(~scanf("%c%d %c%d",&a.c,&a.y,&b.c,&b.y)){
            getchar();
            while(!q.empty()) q.pop();
            memset(vist,0,sizeof(vist));
            a.x=a.c-'a'+1;
            b.x=b.c-'a'+1;
            q.push(a);
            bfs();
        }
        return 0;
    }

     手工版

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define N 510
    int ax[8]={-2,-2,-1,-1,1,1,2,2};
    int ay[8]={-1,1,-2,2,-2,2,-1,1};
    int n,m,sx,sy,ex,ey,step[N<<2],dx[N<<2],dy[N<<2];
    bool vis[N][N];
    char c1[3],c2[3];
    void bfs(){
        int head=0,tail=1;
        dx[1]=sx;
        dy[1]=sy;
        vis[sx][sy]=1;
        while(head<tail){
            head++;
            int x0=dx[head],y0=dy[head];
            for(int i=0;i<8;i++){
                int x1=x0+ax[i],y1=y0+ay[i];
                if(!vis[x1][y1]&&x1>0&&x1<=8&&y1>0&&y1<=8)    {
                    vis[x1][y1]=1;
                    dx[++tail]=x1;dy[tail]=y1;
                    step[tail]=step[head]+1;
                    //qx[dx[tail]]=dx[head];qy[dy[tail]]=dy[head];//记录前驱->倒序输出 
                    if(x1==ex&&y1==ey){
                        printf("To get from %s to %s takes %d knight moves.
    ",c1,c2,step[tail]);
                        return ;
                    } 
                }
            }
        }
    }
    int main(){
        while(scanf("%s%s",c1,c2)==2){
            memset(vis,0,sizeof vis);        
            memset(step,0,sizeof step);    
            sx=c1[0]-'a'+1;
            sy=c1[1]-'0';
            ex=c2[0]-'a'+1;
            ey=c2[1]-'0';
            if(sx==ex&&sy==ey){printf("To get from %s to %s takes %d knight moves.
    ",c1,c2,0);continue;}
            bfs();
        }
        return 0;
    }
  • 相关阅读:
    LUOGU P3355 骑士共存问题(二分图最大独立集)
    LUOGU P1453 城市环路(基环树+dp)
    BZOJ 1441 Min (裴蜀定理)
    LUOGU P1342 请柬(最短路)
    LUOGU P1186 玛丽卡
    LUOGU P2580 于是他错误的点名开始了(trie树)
    l洛谷 NOIP提高组模拟赛 Day2
    bzoj 4372 烁烁的游戏——动态点分治+树状数组
    bzoj 3730 震波——动态点分治+树状数组
    hdu 5909 Tree Cutting——点分治(树形DP转为序列DP)
  • 原文地址:https://www.cnblogs.com/shenben/p/5709975.html
Copyright © 2011-2022 走看看