# A Knight&#39;s Journey

+关注继续查看

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<cstring>
3 #include<algorithm>
4 using namespace std;
5
6 int path[88][88], vis[88][88], p, q, cnt;
7 bool flag;
8
9 int dx[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
10 int dy[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
11
12 bool judge(int x, int y)
13 {
14     if(x >= 1 && x <= p && y >= 1 && y <= q && !vis[x][y] && !flag)
15         return true;
16     return false;
17 }
18
19 void DFS(int r, int c, int step)
20 {
21     path[step][0] = r;
22     path[step][1] = c;
23     if(step == p * q)
24     {
25         flag = true;
26         return ;
27     }
28     for(int i = 0; i < 8; i++)
29     {
30         int nx = r + dx[i];
31         int ny = c + dy[i];
32         if(judge(nx,ny))
33         {
34
35             vis[nx][ny] = 1;
36             DFS(nx,ny,step+1);
37             vis[nx][ny] = 0;
38         }
39     }
40 }
41
42 int main()
43 {
44     int i, j, n, cas = 0;
45     scanf("%d",&n);
46     while(n--)
47     {
48         flag = 0;
49         scanf("%d%d",&p,&q);
50         memset(vis,0,sizeof(vis));
51         vis[1][1] = 1;
52         DFS(1,1,1);
53         printf("Scenario #%d:\n",++cas);
54         if(flag)
55         {
56             for(i = 1; i <= p * q; i++)
57                 printf("%c%d",path[i][1] - 1 + 'A',path[i][0]);
58         }
59         else
60             printf("impossible");
61         printf("\n");
62         if(n != 0)
63             printf("\n");
64     }
65     return 0;
66 }  

7837 0

2634 0

10211 0

9562 0

11663 0
windows server 2008阿里云ECS服务器安全设置

8167 0

4497 0

21019 0

6428 0
+关注

543

0

《2021云上架构与运维峰会演讲合集》

《零基础CSS入门教程》

《零基础HTML入门教程》