• poj 2488 DFS


    A Knight's Journey

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 10   Accepted Submission(s) : 4
    Problem 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
     

    如图,对于骑士所在的位置,所标识的1 2 3 4...位置就是组成字典序的顺序。
    View Code
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 
     6 int vis[26][26],path[26][2];
     7 int flag,a,b;
     8 
     9 int dir[8][2]={-2,-1,-2,1,-1,-2,-1,2,    //这样子排数字是为了保证字典序
    10                 1,-2, 1,2, 2,-1, 2,1};
    11 
    12 void dfs(int s,int t, int k)
    13 {
    14     int i,j;
    15     if(k==a*b)
    16     {
    17         for(i=0;i<k;i++)
    18             printf("%c%d",path[i][0]+'A',path[i][1]+1);
    19         printf("\n");
    20         flag=1;
    21     }
    22     else 
    23     {
    24         for(j=0;j<8;j++)
    25         {
    26             int n=s+dir[j][0];
    27             int m=t+dir[j][1];
    28             if(n>=0 && n<b && m>=0 && m<a && vis[n][m]==0 && !flag)
    29             {
    30                 vis[n][m]=1;
    31                 path[k][0]=n;
    32                 path[k][1]=m;
    33                 dfs(n,m,k+1);
    34                 vis[n][m]=0;
    35             }
    36         }
    37     }
    38 }
    39 
    40 int main()
    41 {
    42     int n,t;
    43     scanf("%d",&n);
    44     for(t=1;t<=n;t++)
    45     {
    46         flag=0;
    47         int i,j;
    48         scanf("%d%d",&a,&b);
    49         for(i=0;i<a;i++)
    50         {
    51             for(j=0;j<b;j++)
    52             {
    53                 vis[i][j]=0;
    54             }
    55         }
    56         vis[0][0]=1;
    57         path[0][0]=0;path[0][1]=0;
    58         printf("Scenario #%d:\n",t);
    59         dfs(0,0,1);
    60         if(flag==0) printf("impossible\n");
    61         printf("\n");
    62     }
    63     //system("pause");
    64     return 0;
    65 }
  • 相关阅读:
    第十次作业
    第十次作业--找回感觉的练习
    MySQL 执行查询报错:Fatal error encountered during command execution
    ASP.NET MVC 4 创建、编辑Model时的数据绑定
    如何解决System.Web.HttpRequestValidationException的异常
    C# winform 跨线程更改窗体控件的属性
    使用NPOI读取Excel报错ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature
    委托和事件简单样例
    SQLServer 2008 R2 对同时含有数字和中文的字段进行排序
    winform中选择文件获取路径
  • 原文地址:https://www.cnblogs.com/shenshuyang/p/2608999.html
Copyright © 2020-2023  润新知