• A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏


    A Knight’s Journey
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 35564 Accepted: 12119

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

    Input
    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, …

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

    Sample Input

    3
    1 1
    2 3
    4 3

    Sample Output

    Scenario #1:
    A1

    Scenario #2:
    impossible

    Scenario #3:
    A1B3C1A2B4C2A3B1C3A4B2C4

    Source
    TUD Programming Contest 2005, Darmstadt, Germany

    一段时间没有做搜索,果然手生了很多,犯了各种各样的错误,要赶紧补回来.

    #include <map>
    #include <set>
    #include <list>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <string>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define eps 1e-9
    #define LL long long
    #define PI acos(-1.0)
    #define INF 0x3f3f3f3f
    #define CRR fclose(stdin)
    #define CWW fclose(stdout)
    #define RR freopen("input.txt","r",stdin)
    #pragma comment(linker,"/STACK:102400000")
    #define WW freopen("output.txt","w",stdout)
    
    struct node
    {
        int x;
        int y;
    } a[100];
    
    int Dir[][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
    
    bool vis[9][9];
    
    int n,m;
    
    int flag;
    
    bool DFS(int x,int y,int site)
    {
        vis[x][y]=true;
        a[site].x=x;
        a[site].y=y;
        if(site==n*m)
        {
            flag=true;
            return true;
        }
        int Fx,Fy;
        for(int i=0; i<8; i++)
        {
            Fx=x+Dir[i][0];
            Fy=y+Dir[i][1];
            if(Fx>=0&&Fx<n&&Fy>=0&&Fy<m&&!vis[Fx][Fy])
            {
                if(DFS(Fx,Fy,site+1))
                {
                    return true;
                }
    
            }
        }
        vis[x][y]=false;
        return false;
    }
    
    int main()
    {
    
        int T,w=1;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d %d",&n,&m);
            flag=false;
            memset(vis,false,sizeof(vis));
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(DFS(i,j,1))
                    {
                        flag=true;
                        break;
                    }
                }
                if(flag)
                {
                    break;
                }
            }
            printf("Scenario #%d:
    ",w++);
            if(flag)
            {
                for(int i=1; i<=n*m; i++)
                {
                    printf("%c%c",a[i].y+'A',a[i].x+1+'0');
                }
                printf("
    
    ");
            }
            else
            {
                printf("impossible
    
    ");
            }
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    win10应用 UWP 使用MD5算法
    win10应用 UWP 使用MD5算法
    用git上传代码到新浪云
    用git上传代码到新浪云
    redis三节点sentinel部署
    [HNOI/AHOI2018]转盘
    用git上传代码到新浪云
    Windows-universal-samples-master示例 XamlCommanding
    Windows-universal-samples-master示例 XamlCommanding
    Windows-universal-samples-master示例 XamlCommanding
  • 原文地址:https://www.cnblogs.com/juechen/p/4721919.html
Copyright © 2020-2023  润新知