Poj 2488 -- a knight's journey

Source: Internet
Author: User
A knight's journey
Time limit:1000 ms   Memory limit:65536 K
Total submissions:30388   Accepted:10404

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

Let the server guard traverse the entire graph in Lexicographic Order. Each vertex can only be reached once.

Train of Thought: the data is not big, just search directly. Note that each test data is followed by a blank line.

1/* ============================================== ====================================== 2 * Author: kevin 3 * filename: aknightsjourney. CPP 4 * creat time: 5 * description: 6 ================================================ ===================================== */7 # include <iostream> 8 # include <algorithm> 9 # include <cstdio> 10 # include <cstring> 11 # include <queue> 12 # include <cmath> 13 # define CLR (, b) Memset (a, B, sizeof (A) 14 # define M 3015 using namespace STD; 16 struct node17 {18 int X, Y; 19} steps [M * m]; 20 bool vis [m] [m]; 21 int p, q; 22 int dir [8] [2] = {-1,-2}, {1, -2}, {-2,-1}, {2,-1 }}; 23 int DFS (int x, int y, int CNT) 24 {25 if (vis [x] [Y]) {26 return-1; 27} 28 vis [x] [Y] = true; 29 steps [CNT]. X = x; steps [CNT]. y = y; 30 if (CNT = p * q-1) return 1; 31 for (INT I = 0; I <8; I ++) {32 in T xx = x + dir [I] [0]; 33 int YY = Y + dir [I] [1]; 34 if (XX> = 1 & XX <= P & YY> = 1 & YY <= q) {35 int ans = DFS (XX, YY, CNT + 1); 36 IF (ANS = 1) {37 return true; 38} 39 else if (ANS = 0) {40 vis [XX] [YY] = false; 41} 42} 43} 44 return 0; 45} 46 int main (INT argc, char * argv []) 47 {48 int N, T = 1; 49 scanf ("% d", & N); 50 while (n --) {51 CLR (steps, 0 ); 52 scanf ("% d", & P, & Q); 53 int CNT = 0; 54 printf ("Scenario # % d: \ n ", t ++ ); 55 int flag = 0; 56 for (INT I = 1; I <= Q; I ++) {57 for (Int J = 1; j <= P; j ++) {58 CLR (VIS, 0); 59 If (DFS (J, I, CNT) = 1) {// because I and j are reversed, contributed several times wr60 flag = 1; 61 break; 62} 63} 64 if (FLAG) break; 65} 66 If (! Flag) {67 printf ("impossible"); 68} 69 else {70 for (INT I = 0; I <Q * P; I ++) {71 printf ("% C % d", steps [I]. y-1 + 'A', steps [I]. x); 72} 73} 74 printf ("\ n"); 75} 76 return 0; 77}
View code

 

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.