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.
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 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, . . .
Output
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.
Sample Input
If no such path exist, you should output impossible on a single line.
3 1 1 2 3 4 3Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
背景
骑士厌倦了一次又一次地看到同样的黑白方块,决定去旅行全世界。每当骑士移动,它是两个正方形在一个方向和一个正方形垂直于这个。
骑士的世界就是他赖以生存的棋盘。我们的骑士住在棋盘上,棋盘的面积比普通的8*8棋盘小,但仍然是长方形的。你能帮这位爱冒险的骑士制定旅行计划吗?
问题
找一条路让骑士每一个广场参观一次。骑士可以在棋盘的任何一个方块上开始和结束。
输入
输入从第一行的正整数n开始。以下几行包含n个测试用例。每个测试用例由一行组成,其中有两个正整数p和q,这样1<=p*q<=26。这表示一个p*q棋盘,
其中p描述有多少不同的方块数字1。P存在,Q描述有多少不同的正方形字母存在。这些是拉丁字母表的第一个q字母:A。
输出
每个场景的输出都以包含“scenario#i:”的行开头,其中i是从1开始的场景数。然后打印一条包含字典路径的单行,该路径访问棋盘的所有棋子,后面跟
着一个空行。路径应该通过连接访问的正方形的名称在单行上给出。每个正方形名称都由一个大写字母和一个数字组成。如果不存在这样的路径,则应该在单行
上输出不可能的。
思路:
一道dfs的题。大概就是建立图,然会用深度优先搜索。一共有八个方向,直到步数s等于地图的格数,便是马将整个图遍历完。输出按字典序,直接从第一格开
始搜素可以解决这个问题。
代码:
#include
#include
using namespace std;
struct map{
char c;
int r;
}mi[900];
int row,column;
int flag=0;
int book[30][30]={0};
int next[8][2]={-1,-2,1,-2,-2,-1,2,-1,-2,1,2,1,-1,2,1,2};//八个方向
void dfs(int m,int n,int s){//s记录是否走完
mi[s].r=m;
mi[s].c='A'+n-1;
if(s==row*column){
flag=1;
return;
}
for(int i=0;i<8;i++){
int sm=m+next[i][0];
int sn=n+next[i][1];
if(sm>0&&sn>0&&sm<=row&&sn<=column&&book[sm][sn]==0&&flag==0){
book[sm][sn]=1;
dfs(sm,sn,s+1);
book[sm][sn]=0;//最后将图回归为0,便于下一张图建立时的遍历
}
}
}
int main(){
int t,ft=1;
scanf("%d",&t);
while(t--){
scanf("%d%d",&row,&column);
book[1][1]=1;//从第一个开始可以保证按字母序列输出
dfs(1,1,1);
printf("Scenario #%d:\n",ft++);
if(flag==1){
for(int i=1;i<=row*column;i++){
printf("%c%d",mi[i].c,mi[i].r);
}
printf("\n\n");
}
else
printf("impossible\n\n");
flag=0;
}
return 0;
}