Description
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.
InputThe 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, . . .
OutputThe 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 Input3
1 1
2 3
4 3
Sample OutputScenario #1:
A1Scenario #2:
impossibleScenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
*/马走日,象走田;
车走直路炮翻山;
士走斜线护将边;
小卒一去不回还。*/
#include<iostream>
#include<string.h>
using namespace std;
int mapp[27][27], book[27][27];//mapp记录路径,book记录位置是否被访问,我用map竟然会报错。。。。
int flag,p,q;//p,q一定要定义全局变量
int nexxt[8][2] = { {-1,-2},{1,-2},{-2,-1},{2,-1},
{-2,1},{2,1},{-1,2},{1,2} };//马走日,8个方向
void dfs(int x, int y, int step)
{//关键所在!!!最小遍历路径按字典序排序肯定是从第一个点开始的,因为第一个点为A1,字典序最小
mapp[step][0] = x;//存储路径,这个是数字坐标
mapp[step][1] = y;//这个是字母坐标
//可以拿笔模拟一下,就清楚明白了(滑稽)
if (step == p * q)
{//遍历完成
flag = 1;
return;
}
for (int k = 0; k <= 7; k++)
{//从8个方向寻找
int tx = x + nexxt[k][0];
int ty = y + nexxt[k][1];
if (tx >= 1 && tx <= p && ty >= 1 && ty <= q && !book[tx][ty] && !flag)
{//边界条件一定要找对,因为是从(1,1)开始的,得带=;没被标记过,没到终点!!!
book[tx][ty] = 1;//标记该点走过;
dfs(tx, ty, step + 1);//从该点开始,步数加一
book[tx][ty] = 0;//取消标记,之前说过原因
}
}
}
int main()
{
int sd, s = 1;
cin >> sd;
while (sd--)
{//初始化标记量
flag = 0;
cin >> p >> q;
memset(book, 0, sizeof(book));//每次初始化标记数组为0,未标记
book[1][1] = 1;//标记,从第一个点开始
dfs(1, 1, 1);//包括第一个点,步数为1>>>>搜索完毕
printf("Scenario #%d:\n", s++);//注意输出样式
if (flag)//如果遍历完毕,就输出路径
{
for (int i = 1; i <= p * q; i++)
//关键所在,遍历整个地图,把前一个坐标转换为字母,后一个原样输出q
//先输出字母坐标,后输出数字坐标!!!!
printf("%c%d", mapp[i][1] - 1 + 'A', mapp[i][0]);
//注意是从1开始的,要先减1再加'A'
cout << endl << endl;//换两次行
}
else//不可能遍历
cout << "impossible" << endl << endl;
}
}
inline char gc()
{
static char buff[100000000], *S = buff, *T = buff;
return S == T && (T = (S = buff) + fread(buff, 1, 100000000, stdin), S == T) ? EOF : *S++;
}