• H


    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
    题目意思:
    在一个给定大小的国际象棋的棋盘,上面有一个马,问马可以走完棋盘吗?


    解法:

     1 #include <iostream>
     2 #include <string.h>
     3 using namespace std;
     4 
     5 const int MAX = 300 +10;
     6 int dx[8]={-2,-2,-1,-1,1,1,2,2};
     7 int dy[8]={-1,1,-2,2,-2,2,-1,1};
     8 int L,W;
     9 int Map[MAX][MAX];
    10 int flag;
    11 int str1[MAX],str2[MAX];
    12 
    13 void print()
    14 {
    15     for(int i = 1;i <= L;i++)
    16         for(int j = 1;j <= W;j++)
    17         {
    18             str1[Map[i][j] ] = i;
    19             str2[Map[i][j] ] = j;
    20         }
    21 }
    22 
    23 void Dfs (int x,int y,int count1)
    24 {
    25     if(count1 == L*W)
    26     {
    27         flag = 1;
    28         print();
    29         return;
    30     }
    31     for(int i = 0;i < 8;i++)
    32     {
    33         if(flag) return;
    34         else
    35         {
    36             int x1 = x+dx[i];
    37             int y1 = y+dy[i];
    38             if(x1>=1 && x1<=L && y1>=1 && y1<=W && !Map[x1][y1] )
    39             {
    40        //         cout<<"x1 = "<<x1<<" y1 = "<<y1<<" count = "<<count1<<endl;
    41                 Map[x1][y1] = count1 + 1;
    42                 Dfs(x1,y1,count1+1);
    43                 Map[x1][y1] = 0;
    44             }
    45         }
    46     }
    47 
    48     return ;
    49 }
    50 
    51 int main()
    52 {
    53     int n;
    54     cin>>n;
    55     int n0 = 0;
    56     while(n--)
    57     {
    58         cin>>W>>L;
    59         cout<<"Scenario #"<<++n0<<":"<<endl;
    60         memset(Map,0,sizeof(Map));
    61         flag = 0;
    62         Map[1][1] = 1;
    63         Dfs(1,1,1);
    64 
    65         if(flag == 0)
    66             cout<<"impossible"<<endl;
    67         else
    68         {
    69             for(int i = 1;i <= L*W;i++)
    70             {
    71                 char temp = str1[i] +'A'-1;
    72                 cout<<temp<<str2[i];
    73             }
    74             cout<<endl;
    75         }
    76         cout<<endl;
    77     }
    78 
    79     return 0;
    80 }
  • 相关阅读:
    Oracle 分析函数(Analytic Functions) 说明
    Build Your Own Oracle RAC 10g Release 2 Cluster on Linux and FireWire
    Build Your Own Oracle RAC 10g Release 2 Cluster on Linux and FireWire
    ORACLE SEQUENCE 介绍
    RAC Ocfs2文件系统常见问题解决方法
    linux 下修改日期和时间
    寒假刷题之7——波纹
    iOS 游戏 Oh my fish! 切割痕迹实现
    ACM常识
    寒假刷题之6——迷宫
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7240381.html
Copyright © 2020-2023  润新知