描述
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
这题挺有意思的,国际象棋马的走法。
可以与红与黑区别一下
辨析:
注意该题与红与黑类似,但有一点不同。
红与黑相当于从一点出发可以遍历图中点的个数,该点可以不是这条路径的起点,
也就是说他拓展的时候,会出现多个节点的出度为0(即该点没有引申的边)。
而这题的话,只能是最终的节点出度为0,其他所有节点都必须不为0。
此外,它也是从一点出发,但它要求的是形成一条以A1为起点的路径,该路径包含图的所有点。
如图 红与黑的结果可以是1,2两种情况,这题的结果只能是2。
因此这题vis标记后需要复原。
然后就普通做法了
贴代码:
#include<iostream>
#include<cstring>
using namespace std;
const int N = 100;
int p,q;
char path[N]; //记录路径
bool vis[N][N]; //是否经过
int cnt = 1; //记录步数
int dis[8][2] = {{-2,1},{-2,-1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; //8个方向,这样的顺序保证每个节点都是当前最优
bool f = false; //是否找到解
void Init() //初始化
{
f = false;
memset(path,0,sizeof(path));
path[0] = path[1] = '1'; //最开始的路径加进去,因为存在一条路径的话,A1开头肯定最小
cnt = 2;
memset(vis,0,sizeof(vis));
cin >> p >> q;
}
bool Judge(int x,int y) //判断该节点是否可以拓展
{
if(x<1 || x>q || y<1 || y>p) //越界
return false;
if(vis[x][y]) //访问过?
return false;
return true;
}
int now_x,now_y;
void Print() //输出
{
// cout << cnt << endl;
for(int i = 0; i<cnt; ++i) {
if((i&1) == 0)
cout <<(char)(path[i]-'1'+'A');
else
cout <<path[i];
}
cout <<endl;
}
void Dfs(int x,int y) //深度优先遍历寻找解,由于dis数组的缘故,保证当前拓展节点最优
{
if(f) //若存在解,直接返回
return;
if(x<1 || x>q || y<1 || y>p)//越界
return;
vis[x][y] = 1; //记录该节点已访问
if(cnt == 2*q*p) { //表明已经加入了所有节点
f = true;
Print();
return;
}
for(int i = 0; i<8; ++i) { //对8个方向拓展
now_x = x + dis[i][0];
now_y = y + dis[i][1];
if(Judge(now_x,now_y)) {
path[cnt++] = now_x + '0';
path[cnt++] = now_y + '0';
Dfs(now_x,now_y);
cnt -= 2;
}
}
vis[x][y] = 0; //这步很关键,因为当前的节点可能导致误解,要走下一种
return;
}
int main()
{
ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n;
cin >> n;
for(int i = 1; i<=n; ++i) {
Init();
cout << "Scenario #" << i <<":" << endl;
Dfs(1,1);
if(!f)
cout << "impossible" << endl;
cout <<endl;
}
return 0;
}
加油!!别停下
欢迎交流!!