A Knight's Journey
POJ - 2488
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<algorithm>
#include <cstring>
#include <string>
#define INF 0x7f7f7f7f
typedef long long ll;
using namespace std;
bool visit[100][100];
bool flag;//标记是否已完成遍历。
int p = 1;
int m, n;
int t;
int move[8][2] = {-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};//移动位置的数组,顺序不能变,因为题目要求字典序。
char str[10000];
void dfs(int x, int y, int c)
{
if (flag)//已完成,直接返回
{
return;
}
str[c] = x + 'A';//将该位置写入数组
str[c+1] = y + '1';
c += 2;//游标移动
if (c == m*n*2)//完成遍历则打印路径
{
flag = 1;
printf("Scenario #%d:\n", p);
printf("%s\n", str);
return;
}
for (int i = 0; i < 8; i++)//遍历八种跳法
{
int nx = x + move[i][0], ny = y + move[i][1];
if (0<=nx&&nx<n&&0<=ny&&ny<m&&!visit[nx][ny])//符合条件,深搜该位置
{
visit[nx][ny] = true;
dfs(nx, ny, c);
visit[nx][ny] = false;//回溯
}
}
c -= 2;//回溯
str[c] = 0;
str[c+1] = 0;
}
int main()
{
scanf("%d", &t);
while (t--)
{
for (int i = 0; i < 200; i++)
{
str[i] = 0;
}
scanf("%d%d",&m,&n);
flag = 0;
for (int i = 0; i < n; i++)//从任意地点开始,遍历整个图(其实不用,只要从A1开始搜就可以了,因为如果该棋盘有解,必定经过A1,所以不需要遍历图。此处写麻烦了。)
{
for (int j = 0; j < m; j++)
{
if (flag)
{
break;
}
else
{
memset(visit, false, sizeof(visit));//初始化访问数组。
visit[i][j] = true;//将i,j位置设为true
dfs(i, j, 0);//从i,j开始搜索
}
}
}
if (!flag)
{
printf("Scenario #%d:\n", p);
printf("impossible\n");
}
if (t)//注意换行符的打印
{
printf("\n");
}
p++;
}
return 0;
}