Address: a knight's journey
Question:
The server guard follows the pattern of the day and gives you the p * q board. You can ask if the server guard can travel all the positions of the board and output the route sequence of the server guard (1. 2 ....) q (. B ...)
Output in Lexicographic Order. If 'impossible 'cannot be output '.
Solution:
Search. Because it is to traverse all vertices in the full graph, it must pass through A1. and since A1 can pass through, it must start with A1.
Note: 1. According to the lexicographic output meaning (first an A. B. C...) sort the output.
Const int dirx [] = };
Const int diry [] = {-2,-2,-1,-1, 1, 2 };
2. There is a line break after each group of data.
Code:
1 # include <algorithm> 2 # include <iostream> 3 # include <sstream> 4 # include <cstdlib> 5 # include <cstring> 6 # include <cstdio> 7 # include <string> 8 # include <bitset> 9 # include <vector> 10 # include <queue> 11 # include <stack> 12 # include <cmath> 13 # include <list> 14/ /# include <map> 15 # include <set> 16 using namespace STD; 17 /************************************** */18 # define ll long 19 # Define int64 _ int64 20 # define PI 3.1415927 21 /*************************** * **********/22 const int INF = 0x7f7f7f7f; 23 const double EPS = 1e-8; 24 const double PIE = ACOs (-1.0); 25 const int d1x [] = {0,-, 1 }; 26 const int d1y [] = {-,}; 27 const int d2x [] = {0,-, 1}; 28 const int d2y [] =, -}; 29 const int FX [] = {-1,-1,-,}; 30 const int FY [] = {-, 1, -,-, 1}; 31/* VEC Tor <int> map [N]; Map [A]. push_back (B); int Len = map [v]. size (); */32 /************************************ * **/33 void openfile () 34 {35 freopen ("data. in "," rb ", stdin); 36 freopen (" data. out "," WB ", stdout); 37} 38 priority_queue <int> qi1; 39 priority_queue <int, vector <int>, greater <int> qi2; 40/*********************** Lili split line, the above is the template section ***************/41 const int dirx [] =, -1, 1}; 42 const Int diry [] = {-2,-2,-1,-, 2}; 43 int vis [30] [30]; 44 int p, q; 45 int CE; 46 struct node 47 {48 int X; 49 char y; 50} node [30]; 51 int DFS (int x, int y, int CNT) 52 {53 int XX, YY; 54if (cnt-1 = p * q) 55 {56 Ce = 1; 57 Return 0; 58} 59 for (INT I = 0; I <8; I ++) 60 {61 xx = x + dirx [I]; 62 YY = Y + diry [I]; 63 If (XX> 0 & XX <= P & YY> 0 & YY <= Q &&! Vis [XX] [YY]) 64 {65 // CNT ++; 66 vis [XX] [YY] = 1; 67 node [CNT]. X = xx; 68 node [CNT]. y = YY; 69 DFS (XX, YY, CNT + 1); 70 vis [XX] [YY] = 0; 71} 72 If (CE) 73 return 0; 74} 75} 76 int main () 77 {78 int CAS; 79 int cntt = 1; 80 scanf ("% d", & CAS); 81 While (CAS --) 82 {83 scanf ("% d", & P, & Q); 84 memset (VIS, 0, sizeof (VIS); 85 memset (node, 0, sizeof (node); 86 int CNT = 1; 87 Ce = 0; 88 node [CNT]. X = 1; 89 node [CNT]. y = 1; 90 vis [1] [1] = 1; 91 DFS (1, 1, CNT + 1); 92 printf ("Scenario # % d: \ n ", cntt ++); 93 If (CE) 94 {95 for (INT I = 1; I <= p * q; I ++) 96 {97 printf ("% C % d", node [I]. Y + 'a'-1, node [I]. x); 98} 99 printf ("\ n"); 100} 101 else102 printf ("impossible \ n"); 103 If (CAS) 104 printf ("\ n"); 105} 106 return 0; 107}
View code