Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18954 | Accepted: 6370 |
Description

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.
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
题意:输入n代表n个测试数据,然后每个测试数据输入p和q,代表长和宽。骑士要在这个p*q的区域内走马字(象棋里的走法)。
问:骑士能否不重复的游遍整个区域?若可以,按字典序输出路径,否则输“impossible”。
输出游历区域,(根据国际象棋标准)行数y用字母ABCD...Z表示,列x用数字1234....p表示。
这里就用回溯搜索dfs,则搜索到的第一个可行的符合要求的路径必定是字典序的。其他解释见代码
#include<iostream> using namespace std; int visited[27][27]; //标记是否访问过 int dir[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; // 骑士移动的8个方向 (按字典序排列) int step,visite; //step表示已游历的区域数目,visite用来记录是否有可行的路径 int x[27],y[27]; // 访问的位置坐标 int n,p,q,i,j; void dfs(int r,int l) //搜索 { int m,n,i; if(visite) return; step++; //step+1 x[step]=r; y[step]=l; //记录当前位置 if(step==p*q) //符合要求 答案已找到 { visite=1; return; } visited[r][l]=1; //标记为已游历 for(i=0;i<8;i++) { m=l+dir[i][0]; // y方向 n=r+dir[i][1]; //x方向 if(visited[n][m]==0&&n>0&&n<=p&&m>0&&m<=q) //下个游历的区域未走过且未超出区域 { dfs(n,m); step--; //回溯不符合,step-1 } } visited[r][l]=0;//返回时,重置为未访问 return; } int main() { cin>>n; for(i=1;i<=n;i++) { step=0; visite=0; cin>>q>>p; dfs(1,1); cout<<"Scenario #"<<i<<":"<<endl; if(visite) { for(j=1;j<=p*q;j++) cout<<(char)(x[j]+64)<<y[j]; /* 国际标准,横行应该是y,但为了适应习惯, 我把输入的p,q换了位置,所以这里输出还是横行为x */ cout<<endl; } else cout<<"impossible"<<endl; if(i!=n) cout<<endl; } return 0; }