Poj 2488 a knight's journey (violent DFS, you know)

Source: Internet
Author: User

Http://poj.org/problem? Id = 2488

A knight's journey
Time limit: 1000 ms   Memory limit: 65536 K
Total submissions: 24572   Accepted: 8305

Description

Background   The knight is getting bored of seeing the same black and white squares 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 between different square numbers 1 ,..., P exist, Q describes how many different square letters exist. these are the first Q letters of the Latin alphabet: ,...

Output

The output for every scenario begins with a line ining "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 shoshould 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 shoshould output impossible on a single line.

Sample Input

 
31 12 34 3

Sample output

Scenario #1: a1scenario #2: impossiblescenario #3: a1b3c1a2b4c2a3b1c3a4b2c4

Analysis:
Here, 1 <= p * q <= 26 The data volume of the question is not very large. It can be solved directly by brute force DFS, mainly to ensure that the first output is the smallest Lexicographic Order, here we can control the order of search traversal, which is mainly reflected in the following array:
Int CY [] = {-2,-2,-1,-1, 1, 2 };
Int CX [] = };
Here, the value of the Cy array is {-2,-2,-1,-, 2} Because Cy is A, B, and C in the output string, it is preferred, then CX {-,-} is arranged one by one
This ensures the minimum Lexicographic Order.
Code:
 1 # Include <iostream> 2 # Include <stdio. h> 3 # Include < String . H> 4   5   Using  Namespace  STD;  6   7   //  This ensures that the final output sequence is the Lexicographic Order.  8   Int CY [] = {- 2 ,- 2 ,- 1 ,- 1 , 1 , 1 , 2 ,2  };  9   Int CX [] = {- 1 , 1 ,- 2 , 2 ,- 2 , 2 ,- 1 , 1  };  10   11   Int Mark [ 30 ] [ 30  ];  12   Int Flag = 0  ;  13   14   Void DFS ( Int N, Int M, Int X, Int Y, Int CNT)  15   {  16       Int  I, J, K;  17       If (FLAG) //  No need to find the sequence  18   {  19           Return  ;  20   } 21       If (CNT = N * m) //  Find the sequence for output  22   {  23           For (K = 1 ; K <= N * m; k ++ )  24           For (I = 1 ; I <= N; I ++ )  25   { 26               For (J = 1 ; J <= m; j ++ )  27   {  28                   If (K = Mark [I] [J])  29   {  30 Printf ( "  % C % d  " ,'  A  ' - 1 + J, I );  31   }  32   }  33   }  34 Printf ( "  \ N  "  );  35 Flag = 1 ; //  Mark indicates a sequence  36           Return  ;  37   }  38       For (I = 0 ; I < 8 ; I ++ )  39   {  40          Int DX = x + CX [I];  41           Int DY = Y + CY [I];  42           If (Dx> = 1 & DX <= N & dy> = 1 & Dy <= M &&! Mark [dx] [dy])  43   {  44 Mark [dx] [dy] = CNT + 1 ;//  Mark the Location status  45 DFS (n, m, dx, Dy, CNT + 1 ); //  Search for the next Vertex  46 Mark [dx] [dy] = 0 ; //  Backtracking  47   }  48   }  49   } 50   51   Int  Main ()  52   {  53       Int  T, K;  54 Scanf ( "  % D  " ,& T );  55       For (K = 1 ; K <= T; k ++ )  56   {  57           Int  N, m;  58 Scanf ( "  % D  " , & N ,& M );  59 Printf ( "  Scenario # % d: \ n  "  , K ); 60           If (N = 1 & M = 1  )  61   {  62 Printf ( "  A1 \ n  "  );  63               Continue  ;  64 }Else   If (N = 1 | M = 1  )  65   {  66 Printf ( "  Impossible \ n  "  );  67               Continue  ;  68  }  69 Memset (mark, 0 , Sizeof  (Mark ));  70           Int  I, J;  71           For (I = 1 ; I <= N; I ++ )  72   {  73               For (J =1 ; J <= m; j ++ )  74   {  75 Mark [I] [J] = 1  ;  76 Flag = 0  ;  77 DFS (n, m, I, J, 1  );  78                   If (FLAG)  79   {  80                       Break  ;  81   }  82 Memset (mark, 0 , Sizeof  (Mark ));  83   }  84               If (J <=M)  85   {  86                   Break  ;  87   }  88   }  89           If (I> N)  90   {  91 Printf ( " Impossible \ n  "  );  92   }  93 Printf ( "  \ N  "  );  94   }  95       Return   0  ;  96 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.