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 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 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
If no such path exist, you should output impossible on a single line.
3 1 1 2 3 4 3Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
由于要字典序最小,则在马走的时候优先走左上的点,这样只要满足条件就可以输出。实则为搜索+贪心
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::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<int>
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#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 #"<<k<<":"<<endl;
memset(book,0,sizeof(book));
memset(mpp,0,sizeof(mpp));
cin >> 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"<<endl;
}
else
cout<<re<<endl;
cout<<endl;
}
}
void dfs(int x,int y,string s,int step)
{
if(ff) return ;
if(step==n*m)
{
if(re.size()==0)
{
re = s;
}
ff = 1;
//cout<<s<<endl;
return ;
}
for(int i=0;i<8;i++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(book[xx][yy]==0&&xx>=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<<tt<<endl;
dfs(xx,yy,tt,step+1);
book[xx][yy] = 0;
}
}
}