题目
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.
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
3
1 1
2 3
4 3
Sample Output
Scenario #1:
A1
Scenario #2:
impossible
Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
题目大意
任选一个起点,按照国际象棋马的跳法,不重复的跳完整个棋盘,如果有多种路线则选择字典序最小的路线(路线是点的横纵坐标的集合)
题目思路
题目要求的是满足条件的解,应该想到用回溯法(回溯法又称解决问的通法).而回溯法最重要的思想就是用DFS遍历解空间树(状态树)。解空间树上的每个节点表示一种状态。对于本题,每个节点的状态就应该是(x,y,step)即骑士所处的位置和已经走了多少步。状态转移的方法就是国际象棋马的跳法,根据马的跳法改变坐标,步数加一。那么从树的根节点开始DFS,如果在某个节点处,骑士仍可以继续前进,那么就DFS遍历它的子树。否则(说明骑士所处位置无法继续跳但还没有走完棋盘所有地点),就要回溯到上层节点继续寻找下一个合适的位置。直到树的高度等于p*q,说明骑士已经走完了棋盘的所有点。
注意,题目说骑士可以从任意点出发,但是结果要求输出按字典顺序最小的路径。对于字典顺序,举个例子就是A1和A2先输出A1,对于B1和A1,先输出A1。如果骑士从某点出发可以走完棋盘所有点,那么他必定要路过A1点,那么他也一定可以从A1点出发走完所有的点,所以我们可以直接让骑士 从A1点出发。
还要注意,国际棋盘列是字母,行是数字。
代码
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
//按照字典序跳转
int direction[8][2] = {
{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}
};
const int MAXN = 30;
int p,q; //棋盘的规格为p*q
struct location{
char x;
char y;
};
bool visit[MAXN][MAXN]; //某个位置是否已经走过
//深度优先搜索
//x,y表示当前骑士的位置,step表示起始已经走的格子数
//way存储结果路径
bool DFS(int x,int y,int step,location *way){
if(step == p*q){ //符合终止状态
for(int i=0;i<p*q;i++){
cout<<way[i].x<<way[i].y;
}
cout<<endl<<endl;
return true;
}
//该节点不符合终止状态,产生新节点
for(int i=0;i<8;i++){
int nx = x + direction[i][0];
int ny = y + direction[i][1];
//如果新产生的节点不符合条件或者被访问过,则跳过
if(nx < 0 || nx >= p || ny < 0 || ny >= q || visit[nx][ny]){
continue;
}
//否则这个新节点符合条件,进行处理
visit[nx][ny] = true;
char col = ny + 'A';
char row = nx + '1';
way[step].x = col;
way[step].y = row;
if(DFS(nx, ny, step + 1,way)){
return true;
}
//如果上面的if语句为真,则return true,那么后面的visit[nx][ny]=false将不执行
//否则,说明骑士走到nx ny后无路可走了,但是还没有走完棋盘所有点,所以这个nx ny节点不能当作结果路径,但我们之前把它设为了true代表我们走过了,所以后面要把她擦除掉
visit[nx][ny] = false;
}
return false; //8个方向都不行则返回false
}
int main()
{
int caseNumber;
scanf("%d",&caseNumber);
for(int i=1;i<=caseNumber;i++){
cin>> p >> q;
memset(visit,false,sizeof(visit));
cout << "Scenario #" << i << ":" <<endl;
location * way = (location *)malloc(sizeof(location)*(p*q+1));
//从"A1"开始
way[0].x = 'A';
way[0].y = '1';
visit[0][0] = true;
if(DFS(0,0,1,way)){
continue;
}else{
cout << "impossible" <<endl<<endl;
}
}
return 0;
}
可能有的人会问,在回溯的时候, way[step].x = col;way[step].y = row这两句已经执行过了,相当于把错误的路径给记录下来了,为什么结果是正确的呢?这是因为,如果骑士走到某点无法继续走下去而回溯,那么它的step是没有变的,等骑士走下一个正确节点时, way[step].x = col;way[step].y = row还会执行,就把之前的错误的路径给覆盖掉了,所以结果是没问题的。
提交后成功AC!!!