• A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏


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

    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

    知识点:DFS

    题意:找到第一条走遍棋盘的路径,并且输出路径。

    难点:扩展状态,按‘日’字形扩展。
    初始状态:从A1开始,个数为1;
    扩展方式:按走‘日’字形8个方向;
    目标状态:棋盘全部遍历到

     1 #include<cstdlib>
     2 #include<cstdio>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cstring>
     6 using namespace std;
     7 char map1[30][30];
     8 int map2[30][30];
     9 int vis[30][30];
    10 int t,p,q,flag;
    11 int dx[] = {-1,1,-2,2,-2,2,-1,1};
    12 int dy[] = {-2,-2,-1,-1,1,1,2,2};
    13 void dfs(int x,int y,int cnt)
    14 {
    15     if(cnt==p*q&&!flag)
    16     {
    17         flag=1;
    18         for(int i=0;i<cnt;i++)
    19             printf("%c%d",map1[i][0],map2[i][0]);
    20             printf("
    
    ");
    21             return;
    22     }
    23     if(x<0||x>=p||y<0||y>=q)
    24         return;
    25     for(int i=0;i<8;i++)
    26     {
    27         int nx=x+dx[i];
    28         int ny=y+dy[i];
    29         //printf("%d @@@%d %d 
    ",ny,nx,cnt);
    30         if(!vis[nx][ny]&&nx>=0&&nx<p&&ny>=0&&ny<q)
    31         {
    32             map1[cnt][0]='A'+ny;
    33             map2[cnt][0]=nx+1;
    34             vis[nx][ny]=1;
    35            // printf("%d %d %d 
    ",ny,nx,cnt);
    36             dfs(nx,ny,cnt+1);
    37             vis[nx][ny]=0;
    38         }
    39     }
    40 
    41 }
    42 int main()
    43 {
    44     scanf("%d",&t);
    45     int ha=0;
    46     while(t--)
    47     {
    48         scanf("%d%d",&p,&q);
    49         memset(vis,0,sizeof(vis));
    50         flag=0;
    51         printf("Scenario #%d:
    ",++ha);
    52         for(int i=0;i<p;i++)
    53             {
    54               for(int j=0;j<q;j++)
    55             {
    56                 if(flag==0)
    57                 {
    58                 map1[0][0]='A'+i;
    59                 map2[0][0]=0+j+1;
    60                 vis[i][j]=1;
    61                 dfs(i,j,1);
    62                 vis[i][j]=0;
    63                 }
    64                 else
    65                     break;
    66             }
    67             if(flag==1)
    68                 break;
    69             }
    70 
    71             if(flag==0)
    72                 printf("impossible
    
    ");
    73     }
    74 
    75     return 0;
    76 }
    77 //3
    78 //1 1
    79 //2 3
    80 //4 3

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

  • 相关阅读:
    奶酪工厂
    P1080 国王游戏(非高精版)
    【洛谷P2150】[NOI2015] 寿司晚宴
    【洛谷P3349】[ZJOI2016]小星星
    【洛谷P5785】[SDOI2012]任务安排
    【模板】严格次短路
    【洛谷P3647】[APIO2014]连珠线
    2021.10.27NOIP模拟总结
    【树形DP】CF1016F Road Projects
    2021CSP-S 总结
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/4639612.html
Copyright © 2020-2023  润新知