• poj 2488 A Knight's Journey(DFS)


    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 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 using namespace std;
     7 const int maxn=26;
     8 int n,p,q;
     9 int dir[8][2]= {-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};//记录8个方向
    10 bool flag,vis[maxn][maxn];//用于标记
    11 int path[maxn][2];
    12 void dfs(int i,int j,int k)
    13 {
    14     path[k][0]=i,path[k][1]=j;
    15     if(k==p*q-1)//走完了
    16     {
    17         flag=true;
    18         return ;
    19     }
    20     for(int u=0; u<8; u++)
    21     {
    22         int a=i+dir[u][0];
    23         int b=j+dir[u][1];
    24         if(!vis[a][b]&&a>=0&&b>=0&&a<q&&b<p)
    25         {
    26             vis[a][b]=true;//标记已走
    27             dfs(a,b,k+1);
    28             if(flag)
    29                 return ;
    30             vis[a][b]=false;//清楚标记
    31         }
    32     }
    33 }
    34 int main()
    35 {
    36     int k=0;
    37     cin>>n;
    38     while(n--)
    39     {
    40 
    41         cin>>p>>q;
    42         printf("Scenario #%d:
    ",++k);
    43         memset(vis,false,sizeof(vis));
    44         memset(path,0,sizeof(path));
    45         for(int i=0; i<q; i++)
    46         {
    47             for(int j=0; j<p; j++)
    48             {
    49                 flag=false;
    50                 vis[i][j]=true;
    51                 dfs(i,j,0);
    52                 if(flag)
    53                     break;
    54                 vis[i][j]=false;
    55             }
    56             if(flag)
    57                 break;
    58         }
    59         if(!flag)
    60             printf("impossible");
    61         else
    62         {
    63             for(int i=0; i<p*q; i++)
    64                 printf("%c%d",path[i][0]+'A',path[i][1]+1);
    65         }
    66         printf("
    
    ");
    67     }
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    虚拟化之基础---虚拟化的的基本了解
    DFS文件服务器实验手册
    CentOS 7下配置ISO镜像文件为本地yum源
    CentOS7中搭建redis集群
    CentOS7中搭建redis单机
    CentOS7搭建zookeeper伪集群
    zookeeper单机安装
    搭建局域网http的yum源
    openstack-学习2-云计算与云计算技术
    openstack-学习1-openstack入门
  • 原文地址:https://www.cnblogs.com/cxbky/p/4853519.html
Copyright © 2020-2023  润新知