Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents
Question link: http://poj.org/problem? Id = 2488
----------------------------------------------------------------------------------------------------------------------------------------------------------
Welcome to tianci Hut: http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
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
General question:
Returns the size of an international board to determine whether the horse can be used.Not repeatedWalkAll cells, And recordLexicographic OrderThe first path.
The Code is as follows:
# Include <cstdio> # include <cstring> # define M 26 struct node {int X, Y;} W [M * m]; bool vis [m] [m]; int p, q; int flag = 0; int dir [8] [2] = {-2,-1}, {-2}, {-1, -2}, {-}, {1,-2}, {}, {2,-1 }}; // The result obtained in this order is the lexicographically ordered bool judge (int x, int Y) {If (x> = 0 & x <Q & Y> = 0 & Y <P &&! Vis [x] [Y]) return true; return false;} void DFS (int x, int y, int step) {W [STEP]. X = x, W [STEP]. y = y; vis [x] [Y] = true; If (step = p * q-1) {flag = 1; return ;}for (INT I = 0; I <8; I ++) {int dx = W [STEP]. X + dir [I] [0]; int DY = W [STEP]. Y + dir [I] [1]; If (Judge (dx, Dy) {vis [dx] [dy] = true; DFS (dx, Dy, step + 1); If (FLAG) // once found, exit the search return; vis [dx] [dy] = false ;}return ;} void print () {for (INT I = 0; I <p * q; I ++) // column is a letter with a behavior number {printf ("% C % d", W [I]. X + 'A', W [I]. Y + 1);} printf ("\ n");} int main () {int T, I, j, CAS = 0; scanf ("% d ", & T); While (t --) {memset (VIS, false, sizeof (VIS); flag = 0; scanf ("% d", & P, & Q); for (I = 0; I <q; I ++) // column {for (j = 0; j <p; j ++) // row {DFS (I, j, 0); If (FLAG) break;} printf ("Scenario # % d: \ n ", ++ CAS); If (FLAG) print (); elseprintf ("impossible \ n");} return 0 ;}