描述
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.
输入
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, . ..
输出
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.
样例输入
3
1 1
2 3
4 3
样例输出
Scenario #1:
A1
Scenario #2:
impossible
Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
来源
TUD Programming Contest 2005, Darmstadt, Germany

1 #include <cstdio> 2 #include <string> 3 #include <memory.h> 4 #include <algorithm> 5 #include <stdlib.h> 6 #include <math.h> 7 #include <iostream> 8 #include<queue> 9 #include <vector> 10 #include <bitset> 11 using namespace std; 12 13 int r, c; 14 int kase; 15 int visited[10][10]; 16 int jing = 0; 17 bool flag; 18 char sign[9] = { 0,'A','B','C','D','E','F','G','H' }; 19 int dir1[8] = { -2,-2,-1,-1,1,1,2,2 }, dir2[8] = {-1,1,-2,2,-2,2,-1,1}; 20 struct node { 21 int x, y; 22 node(int a, int b) :x(a), y(b) {} 23 }; 24 vector<node> solution; 25 void dfs(int x,int y ) { 26 if (jing == r * c) { 27 printf("Scenario #%d: ", kase); 28 vector<node>::iterator i1 = solution.begin(), i2 = solution.end(); 29 for (; i1 != i2; i1++) 30 cout << sign[(*i1).x] << (*i1).y; 31 printf(" "); 32 flag = true; 33 return; 34 } 35 if (flag)return; 36 for (int i = 0; i <= 7; i++) { 37 int xx = x + dir1[i], yy = y + dir2[i]; 38 if (visited[xx][yy]||xx<1||xx>r||yy<1||yy>c) 39 continue; 40 visited[xx][yy] = 1; 41 solution.push_back(node(xx, yy)); 42 jing++; 43 dfs(xx, yy); 44 if (flag == true) 45 return; 46 solution.pop_back(); 47 visited[xx][yy] = 0; 48 jing--; 49 } 50 } 51 52 int main() 53 { 54 int t; 55 scanf("%d", &t); 56 for(kase=1;kase<=t;kase++){ 57 scanf("%d%d", &c, &r); 58 solution.clear(); 59 memset(visited, 0, sizeof(int) * 10 * 10); 60 jing = 1; 61 flag = false; 62 visited[1][1] = 1; 63 solution.push_back(node(1, 1)); 64 dfs(1, 1); 65 if (flag == false) { 66 printf("Scenario #%d: ", kase); 67 printf("impossible "); 68 } 69 } 70 return 0; 71 }
输出格式