寒假训练——POJ - 2488 A Knight's Journey 搜索+贪心

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


由于要字典序最小,则在马走的时候优先走左上的点,这样只要满足条件就可以输出。实则为搜索+贪心

#include  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull; 
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int dir[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};

int ff=0;
void dfs(int x,int y,string s,int step);
string re;
int book[10][10];
int mpp[10][10];
int n,T,m;
int main()
{
	ios::sync_with_stdio(false);
	re.clear();
	cin  >> T;
	for(int k=1;k<=T;k++)
	{
		re.clear();
		cout<<"Scenario #"<> n>>m;
		int flag  =0 ;
		for(int i=1;i<=n;i++)
		{
			if(flag) break;
			for(int j=1;j<=m;j++)
			{
				string ss;
				ss.clear();
				ss +=(char)(j+'A'-1);
				ss +=(char)(i+'0');
				memset(book,0,sizeof(book));
				ff = 0;
				book[i][j]=1;
				dfs(i,j,ss,1);
				book[i][j]=0;
				if(re.size()>=0)
				{
					flag = 1;
					break;
				}
					
			}	
		} 
		if(re.size()==0)
		{
			cout<<"impossible"<=1&&xx<=n&&yy>=1&&yy<=m)
		{
			book[xx][yy]=1;
			string tt = s;
			tt +=(yy+'A'-1);
			//tt +=(yy+'0');
			tt +=(xx+'0');
			
			//cout<





你可能感兴趣的:(ACM_搜索)