题目描述
A Knight’s Journey
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 62878 Accepted: 21576
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
中文大意:
给出一个p行q列的国际棋盘,马可以从任意一个格子开始走,问马能否不重复的走完所有的棋盘。如果可以,输出按字典序排列最小的路径。打印路径时,列用大写字母表示(A表示第一列),行用阿拉伯数字表示(从1开始),先输出列,再输出行。
算法
深度优先搜索+字典序
思路
单独创建一个输出函数,用来进行数据的输出,还有就是字符的处理上,将列仍然看成数字,然后在搜索函数里在转换成字符,以便存储位置。搜索的形式是(int x,int y,int step),其中xy表示几行几列(据y由y+’A’-1算出字符列),起点是(1,1,1).从头开始搜索,结束是如果x*y==step时,则搜索到结果,此时可以输出,在这个判断输出的问题上(即是否可以搜索到!)可以用一个flag标志,还有就是字典序的控制,一定要严格按照顺序进行搜索。
C++代码
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=30;
struct Point {
int shuzi;
char zimu;
} pp[MAXN*MAXN];//记录当前格子信息,用于打印路径
int dir[8][2]= {
{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}
};//严格优先按照y字典序搜索,x数字搜索
//int dir[8][2]={-1,-2,1,-2,-2,-1,2,-1,-2,1,2,1,-1,2,1,2};
bool vis[MAXN][MAXN];
int q,p,cas,flag=0,t;
void dayin(){
cout<<"Scenario #"<<cas<<":"<<endl;
for(int i=1;i<=p*q;i++)
cout<<pp[i].zimu<<pp[i].shuzi;
cout<<endl;
if(t!=0)
cout<<endl;//需注意,若非最后一行中间有一行空行
}
bool judge(int x,int y){//判断是否符合条件
if(vis[x][y]==0&&x>0&&x<=p&&y>0&&y<=q&&flag==0){
return true;
}else{
return false;
}
}
//x,y当前格子,step总步数
void dfs(int x,int y,int step){
pp[step].shuzi=x;
pp[step].zimu='A'+y-1;
if(step==p*q){//退出条件,step==总格子数,则完成搜索,有解
flag=1;
return;
}
for(int i=0;i<8;i++){//按照字典序搜索
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(judge(xx,yy)){
vis[xx][yy]=1;
dfs(xx,yy,step+1);
vis[xx][yy]=0;//因为选最优路径,需恢复现场
}
}
}
int main() {
cas=0;
cin>>t;
while(t--) {
cas++;
cin>>p>>q;
pp[1].shuzi=1;
pp[1].zimu='A';
flag=0;
memset(vis,0,sizeof(vis));
vis[1][1]=1;
dfs(1,1,1);
if(flag) {
dayin();
} else {
cout<<"Scenario #"<<cas<<":"<<endl;
cout<<"impossible"<<endl;
if(t!=0)
cout<<endl;
}
}
return 0;
}