zoukankan      html  css  js  c++  java
  • A Knight's Journey

    题目链接http://noi.openjudge.cn/ch0205/1490/

    描述Background
    The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey
    around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?

    Problem
    Find a path such that the knight visits every square once. The knight can start and end on any square of the board.输入The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .输出The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
    If no such path exist, you should output impossible on a single line.样例输入

    3
    1 1
    2 3
    4 3

    样例输出

    Scenario #1:
    A1
    
    Scenario #2:
    impossible
    
    Scenario #3:
    A1B3C1A2B4C2A3B1C3A4B2C4
    

    来源TUD Programming Contest 2005, Darmstadt, Germany

    WA:

    刚开始思路就错了,我想的是遍历所有的位置,看最后遍历到的总数是不是P*Q。事实上,只要是遍历,最后的结果都是P*Q

    这里其实是搜索出一条路径

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<vector>
    #define DEBUG(x) cout<<#x<<" = "<<x<<endl
    using namespace std;
    int P,Q;
    int visited[10][10];
    int dirx[]={-1,-2,-2,-1,+1,+2,+2,+1};
    int diry[]={-2,-1,+1,+2,-2,-1,+1,+2};
    int cnt=0;
    void dfs(int i,int j)
    {
        if(i<0||j<0||i>=P||j>=Q)return;
        if(visited[i][j])return ;
        visited[i][j]=1;
        cnt++;
        DEBUG(i);
        DEBUG(j);
        printf("
    ");
        for(int k=0;k<8 ;k++ ){
            int ii=i+dirx[k];
            int jj=j+diry[k];
            dfs(ii,jj);
        }
    }
    int main()
    {
        freopen("in.txt","r",stdin);
        int t;
        scanf("%d",&t);
        int n=1;
        while(t--){
            scanf("%d%d",&P,&Q);
            for(int j=0;j<Q ;j++ ){
                for(int i=0;i<Q ;i++ ){
                    memset(visited,0,sizeof(visited));
                    cnt=0;
                    dfs(i,j);
                    DEBUG(i);
                    DEBUG(j);
                    DEBUG(cnt);
                    printf("
    ");
                }
            }
        }
    }

    AC:

    刚开始还是不停的WA,搜了一下,发现原因了

    题目要求以"lexicographically"方式输出,也就是字典序...要以字典序输出路径,那么搜索的方向(我的程序是path()函数)就要以特殊的顺序排列了...这样只要每次从dfs(A,1)开始搜索,第一个成功遍历的路径一定是以字典序排列...

    下图是搜索的次序,马的位置为当前位置,序号格为测试下一步的位置的测试先后顺序

    按这个顺序测试,那么第一次成功周游的顺序就是字典序

     

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<vector>
    #define DEBUG(x) cout<<#x<<" = "<<x<<endl
    using namespace std;
    int P,Q;
    int visited[30][30];
    //int dirx[]={-1,-2,-2,-1,+1,+2,+2,+1};
    //int diry[]={-2,-1,+1,+2,-2,-1,+1,+2};
    int dirx[]={-1,+1,-2,+2,-2,+2,-1,+1};
    int diry[]={-2,-2,-1,-1,+1,+1,+2,+2};
    struct Pos{
        int x;
        int y;
    };
    Pos path[30];
    bool dfs(int i,int j,int step)
    {///已经走了step步,从i,j出发,看是否成功
        if(step==P*Q)return true;
        if(i<0||j<0||i>=P||j>=Q)return false;
        if(visited[i][j])return false;
        visited[i][j]=1;
        path[step].x=i;
        path[step].y=j;
        for(int k=0;k<8 ;k++ ){
            int ii=i+dirx[k];
            int jj=j+diry[k];
            if(dfs(ii,jj,step+1))return true;
        }
        visited[i][j]=0;
        return false;
    }
    int main()
    {
    //    freopen("in.txt","r",stdin);
        int t;
        scanf("%d",&t);
        int n=1;
        while(t--){
            scanf("%d%d",&P,&Q);///P代表数字,Q代表字母,P行Q列
            memset(visited,0,sizeof(visited));
            bool f=false;
            for(int j=0;j<Q ;j++ ){
                for(int i=0;i<P ;i++ ){
                    if(dfs(i,j,0)){
                        f=true;
                        break;
                    }
                }
                if(f)break;
            }
            printf("Scenario #%d:
    ",n++);
            if(f){
               for(int i=0;i<P*Q ;i++ ){
                    printf("%c%d",path[i].y+'A',path[i].x+1);
               }
            }
            else printf("impossible");
            printf("
    
    ");
        }
    }

    参考文献

    https://blog.csdn.net/lyy289065406/article/details/6647666

  • 相关阅读:
    剑指Offer——构建乘积数组
    剑指Offer——把二叉树打印成多行
    剑指Offer——二叉树的下一个结点
    剑指Offer——二叉搜索树与双向链表
    剑指Offer——二叉搜索树的后序遍历序列
    LeetCode——Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode——Construct Binary Tree from Preorder and Inorder Traversal
    剑指Offer——重建二叉树2
    C++ STL基本容器的使用
    mysql中模糊查询的四种用法介绍
  • 原文地址:https://www.cnblogs.com/MalcolmMeng/p/9209499.html
Copyright © 2011-2022 走看看