A Knight's Journey
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26210 Accepted: 8950
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?
![](http://poj.org/images/2488_1.jpg)
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
Source
TUD Programming Contest 2005, Darmstadt, Germany
![](/images/cppblog_com/jaceforce/2488_1fb.jpg)
因为题目要求 print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line.即输出字典序最小的一组答案
字典序最小就是按位比较ASCII码,相等的比较下一位,知道不相等的为止或者长度比另一个长 比如 "abc" < "bbc" "abcd" > "abc" 因为要是有路径的话长度都是一样所以不需要考虑路径长度的问题。
直接考虑每个路径,在搜索的时候只要优先往A副1的方向走,这样获得的第一条路径就是字典序最小的路径了。按照图上的路径。有的题解是转过来的,其实只要写程序的时候注意一下就行了。
这个题目我WA了N多次的原因是Scenario自己打的没有复制,然后输错了。太二了
![](/Images/OutliningIndicators/ContractedBlock.gif)
POJ2488.cpp
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool vis[26][26];
char anx[800],any[800];
char ax[800],ay[800];
int ind = 0;
const int mx[] = {-2,-2,-1,-1,+1,+1,+2,+2};
const int my[] = {-1,+1,-2,+2,-2,+2,-1,+1};
int p,q;
int get = 0;
void push(int x,int y)
{
anx[ind] = x;
any[ind] = y;
ind++;
}
void pop()
{
ind--;
}
bool check(int x,int y)
{
return (((x >= 0 && x < p)&&(y >= 0 && y < q))&&!vis[x][y]);
}
void init()
{
get = 0;
memset(vis,0,sizeof(vis));
ind = 0;
}
bool betteranswer()
{
int bt = 0;
for(int i = 0;i < p*q;i++)
{
if (anx[i] < ax[i])
{
bt = 1;
break;
}
else if (anx[i] == ax[i] && any[i] < ay[i])
{
bt = 1;
break;
}
else if (anx[i] == ax[i] && any[i] == ay[i])
continue;
else
break;
}
return bt;
}
void dfs(int i,int x,int y)
{
push(x,y);
vis[x][y] = 1;
if (i == p*q-1)
{
if (get&& betteranswer())
{
memcpy(ax,anx,sizeof(ax));
memcpy(ay,any,sizeof(ay));
return;
}
else if (!get)
{
get = 1;
memcpy(ax,anx,sizeof(ax));
memcpy(ay,any,sizeof(ay));
}
pop();
vis[x][y] = 0;
return;
}
for(int j = 0;j < 8;j++)
{
int tx = x + mx[j];
int ty = y + my[j];
if (check(tx,ty))
dfs(i+1,tx,ty);
}
pop();
vis[x][y] = 0;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
while(scanf("%d",&T)!=EOF)
{
int f = 0;
for(int i = 1;i <= T;i++)
{
scanf("%d%d",&q,&p);
init();
for(int j = 0;j < p && !get ;j++)
{
for(int k = 0;k < q && !get;k++)
{
dfs(0,j,k);
}
}
if (!f)
{
f = 1;
}
else
{
printf("\n");
}
printf("Scenario #%d:\n",i);
if (get)
{
for(int i = 0;i < p*q;i++)
{
printf("%c%d",ax[i]+65,ay[i]+1);
}
printf("\n");
}
else
{
printf("impossible\n");
}
}
}
return 0;
}