A Knight's Journey
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22289 | Accepted: 7537 |
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.
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
题意:第一行为几个数据,一下几行分别为行和列,问马能不能走完所有的格子,并且按照字典序输出经过的路径……
题解: 利用dfs搜索,注意字典序的输出和格式的输出 应该空一行(a single line)……
代码:#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> using namespace std; const int maxn=100; bool vis[maxn][maxn]; int dir[8][2]={-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};//利用字典序输出时应当注意方向:从字母小的和数字小的方向先搜,然后再按照从左到右的方向再搜…… bool panding; int step; int p,q; char ansx[1000]; int ansy[1000]; bool checkedge(int x,int y) { if(!vis[x][y]&&x<=q&&x>=1&&y<=p&&y>=1) return 1; else return 0; } void dfs(int x,int y,int step) { ansx[step]=x; ansy[step]=y; //将路径存放到数组里 vis[1][1]=1; //这里刚开始疏忽了 if(step==p*q) { panding=1; return; } for(int i=0;i<8;i++) { int tempx=x+dir[i][0]; int tempy=y+dir[i][1]; if(checkedge(tempx,tempy)&&!panding) { vis[tempx][tempy]=1; dfs(tempx,tempy,step+1); vis[tempx][tempy]=0; } } return ; } int main() { int T; cin>>T; int t=1; while(T--) { cin>>p>>q; panding=0; dfs(1,1,1); cout<<"Scenario #"<<t++<<":"<<endl; if(panding) { for(int i=1;i<=p*q;i++) //从1开始 刚开始习惯性的设为0 结果有错误字符 { printf("%c",ansx[i]+'A'-1);// 字典序输出路径 和开始dir的搜索方向有关 cout<<ansy[i]; } cout<<endl<<endl;; //cout<<endl; } else cout<<"impossible"<<endl<<endl; //cout<<endl; } return 0; }