POJ2488 A Knight's Journey 解题报告
A Knight's Journey
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 15815 | Accepted: 5295 |
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
题目大意是给出一个规格小于8*8的棋盘,判断一个只能走“日”的骑士能否不重复的走遍整个棋盘,如果能,按字典序输出走的路径,否则输出“impossible”
这题是一道搜索题,可以用DFS直接解决。每次从左到右,从上到下进行搜索,并标记搜索过的地方;
参考代码:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct prog {
char a ;
int b ;
}tra;
struct proglu{
char str[900];
}map;
bool hash[900];
bool found=false;
int p , q ;
int number=0;
void DFS(prog tmp , int n ,proglu map ,int k)
{
if(found)
return ;
if(n==1)
{
int i;
cout<<"Scenario #"<<number<<":"<<endl;
for(i=0;i<k;i++)
{
cout<<map.str[i];
}
cout<<endl;
found=true;
}
int t=(tmp.a-'A')*30+tmp.b;
hash[t]=true;
prog tmp2;
tmp2=tmp;
tmp2.a-=2;
tmp2.b--;
if(tmp2.a>='A'&&tmp2.b>=1&&hash[(tmp2.a-'A')*30+tmp2.b]==false)
{
map.str[k]=tmp2.a;
map.str[k+1]=tmp2.b+'0';
DFS(tmp2,n-1,map,k+2);
hash[(tmp2.a-'A')*30+tmp2.b]=false;
}
tmp2=tmp;
tmp2.a-=2;
tmp2.b++;
if(tmp2.a>='A'&&tmp2.b<=q&&hash[(tmp2.a-'A')*30+tmp2.b]==false)
{
map.str[k]=tmp2.a;
map.str[k+1]=tmp2.b+'0';
DFS(tmp2,n-1,map,k+2);
hash[(tmp2.a-'A')*30+tmp2.b]=false;
}
tmp2=tmp;
tmp2.a-=1;
tmp2.b-=2;
if(tmp2.a>='A'&&tmp2.b>=1&&hash[(tmp2.a-'A')*30+tmp2.b]==false)
{
map.str[k]=tmp2.a;
map.str[k+1]=tmp2.b+'0';
DFS(tmp2,n-1,map,k+2);
hash[(tmp2.a-'A')*30+tmp2.b]=false;
}
tmp2=tmp;
tmp2.a-=1;
tmp2.b+=2;
if(tmp2.a>='A'&&tmp2.b<=q&&hash[(tmp2.a-'A')*30+tmp2.b]==false)
{
map.str[k]=tmp2.a;
map.str[k+1]=tmp2.b+'0';
DFS(tmp2,n-1,map,k+2);
hash[(tmp2.a-'A')*30+tmp2.b]=false;
}
tmp2=tmp;
tmp2.a+=1;
tmp2.b-=2;
if(tmp2.a<='A'+p-1&&tmp2.b>=1&&hash[(tmp2.a-'A')*30+tmp2.b]==false)
{
map.str[k]=tmp2.a;
map.str[k+1]=tmp2.b+'0';
DFS(tmp2,n-1,map,k+2);
hash[(tmp2.a-'A')*30+tmp2.b]=false;
}
tmp2=tmp;
tmp2.a+=1;
tmp2.b+=2;
if(tmp2.a<='A'+p-1&&tmp2.b<=q&&hash[(tmp2.a-'A')*30+tmp2.b]==false)
{
map.str[k]=tmp2.a;
map.str[k+1]=tmp2.b+'0';
DFS(tmp2,n-1,map,k+2);
hash[(tmp2.a-'A')*30+tmp2.b]=false;
}
tmp2=tmp;
tmp2.a+=2;
tmp2.b-=1;
if(tmp2.a<='A'+p-1&&tmp2.b>=1&&hash[(tmp2.a-'A')*30+tmp2.b]==false)
{
map.str[k]=tmp2.a;
map.str[k+1]=tmp2.b+'0';
DFS(tmp2,n-1,map,k+2);
hash[(tmp2.a-'A')*30+tmp2.b]=false;
}
tmp2=tmp;
tmp2.a+=2;
tmp2.b+=1;
if(tmp2.a<='A'+p-1&&tmp2.b<=q&&hash[(tmp2.a-'A')*30+tmp2.b]==false)
{
map.str[k]=tmp2.a;
map.str[k+1]=tmp2.b+'0';
DFS(tmp2,n-1,map,k+2);
hash[(tmp2.a-'A')*30+tmp2.b]=false;
}
}
int main()
{
int k = 1 ;
int t;
cin>>t;
while(t--)
{
number++;
cin >> q >> p ;
if(number!=1)
cout<<endl;
memset(hash,false,sizeof(hash)) ;
tra.a='A';
tra.b=1;
memset(map.str,0,sizeof(map.str));
map.str[0]='A';
map.str[1]='1';
found=false;
DFS(tra,p*q,map,2);
if(!found)
{
cout<<"Scenario #"<<number<<":"<<endl;
cout<<"impossible"<<endl;
}
}
return 0;
}
作者:ACShiryu
出处:http://www.cnblogs.com/ACShiryu/
若非注明,本博客文章均为原创,版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
该文章也同步发布在我的新浪微博中-ACShiryu's weibo,欢迎收听。
浙公网安备 33010602011771号