DFS poj 2488
A Knight's Journey
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 22060 | Accepted: 7453 |
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.
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;只是次数要注意的是,只需要从一个点出发,就可判断是否可以走遍整个棋盘。因为从其他点出发,得出的步数除了顺序不一样,其他的都是一样的。并没有区别。
另外,不知道是不是电脑的原因,数据跑到10*10就很慢了。。。完全超过了1s,但还是过了。
代码实现:
#include<iostream>
#include<string.h>
using namespace std;
int a[30][30];
char b1[30];
int b2[30];
int ax[8]={-2,-2,-1,-1,1,1,2,2};
int bx[8]={-1,1,-2,2,-2,2,-1,1};
int istrue(int x,int y,int x1,int y1){
if(x<=0||x>y1||y<=0||y>x1)
return 0;
else
return 1;
}
int DFS(int v,int u,int len,int n1,int m1){
if(len==(n1*m1))
return 1;
for(int k=0;k<8;k++){
int tempa=v+ax[k];
int tempb=u+bx[k];
if(istrue(tempa,tempb,n1,m1)){
if(a[tempa][tempb]==1)
continue;
a[tempa][tempb]=1;
b1[len]=tempa+'A'-1;
b2[len]=tempb;
int tmp=DFS(tempa,tempb,len+1,n1,m1);
if(tmp==1)
return 1;
a[tempa][tempb]=0;
}
}
return 0;
}
int main(){
int n,m;
int times=0;
int m1;
cin>>m1;
while(m1--){
cin>>n>>m;
int flag=0;
times++;
// for(int i=1;i<=m;i++){
// for(int j=1;j<=n;j++){
memset(a,0,sizeof(a));
// a[i][j]=1;
a[1][1]=1;
b1[0]=1+'A'-1;
b2[0]=1;
int temp=DFS(1,1,1,n,m);
if(temp==1){
flag=1;
// break;
}
// }
//if(flag==1)
// break;
// }
cout<<"Scenario #"<<times<<":"<<endl;
if(flag==0){
cout<<"impossible"<<endl;
}
else{
for(int g=0;g<n*m;g++){
cout<<b1[g]<<b2[g];
}
cout<<endl;
}
if(m1>0)
cout<<endl;
}
return 0;
}
浙公网安备 33010602011771号