1 #include
2 #include
3 #include<string.h>
4 using namespace std;
5
6 const int MAX_N = 27;
7
8 const int dx[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
9 const int dy[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
10 bool visited[MAX_N][MAX_N];
11 struct Step{
12 char x, y;
13 } path[MAX_N];
14 bool success; //是否成功遍历的标记
15 int cases, p, q;
16
17 void DFS(int x, int y, int num);
18
19 int main()
20 {
21 scanf("%d", &cases);
22 for (int c = 1; c <= cases; c++)
23 {
24 success = false;
25 scanf("%d%d", &p, &q);
26 memset(visited, false, sizeof(visited));
27 visited[1][1] = true; //起点
28 DFS(1, 1, 1);
29 printf("Scenario #%d:\n", c);
30 if (success)
31 {
32 for (int i = 1; i <= p * q; i++)
33 printf("%c%c", path[i].y, path[i].x);
34 printf("\n");
35 }
36 else
37 printf("impossible\n");
38 if (c != cases)
39 printf("\n"); //注意该题的换行
40 }
41 return 0;
42 }
43
44 void DFS(int x, int y, int num)
45 {
46 path[num].y = y + 'A' - 1; //int 转为 char
47 path[num].x = x + '0';
48 if (num == p * q)
49 {
50 success = true;
51 return;
52 }
53 for (int i = 0; i <8; i++)
54 {
55 int nx = x + dx[i];
56 int ny = y + dy[i];
57 if (0 0