跳到正文

个人记录

A Knight's Journey(poj2488)(Journey.cpp)

龙卷风约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年4月18日
更新
2026年4月18日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

题目

A Knight’s Journey

Description

Theknight is getting bored of seeing the same black and white squares again andagain and has decided to make a journey around the world. Whenever a knightmoves, 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 achessboard that has a smaller area than a regular 8 * 8 board, but it is stillrectangular. Can you help this adventurous knight to make travel plans? Problem:Find a path such that the knight visits every square once. The knightcan start and end on any square of the board.

Clipboard Image.png

Input

Theinput begins with a positive integer n in the first line. The following linescontain n test cases. Each test case consists of a single line with twopositive integers p and q, such that 1 <= p * q <= 26. This represents ap * q chessboard, where p describes how many different square numbers 1, …, p exist, q describes how many different square letters exist. These are thefirst q letters of the Latin alphabet: A, …

Output

The output for every scenario begins with a linecontaining “Scenario #i:”, where i is the number of the scenariostarting at 1. Then print a single line containing the lexicographically firstpath that visits all squares of the chessboard with knight moves followed by anempty line. The path should be given on a single line by concatenating thenames of the visited squares. Each square name consists of a capital letterfollowed by a number. If no such path exist, you should output impossible on asingle line.

Sample Input 1

3 1 1 2 3 4 3

Sample Output 1

Scenario #1:

A1

Scenario #2:

impossible

Scenario #3:

A1B3C1A2B4C2A3B1C3A4B2C4

翻译

题目大意:任选一个起点,按照国际象棋马的跳法,不重复的跳完整个棋盘,如果有多种路线则选择字典序最小的路线(路线是点的横纵坐标的集合,注意棋盘的横坐标的用大写字母,纵坐标是数字)否则输出impossibl。

思路 (dfs)

1.很显然,由于是一个棋盘上,

于是可以枚举起点,再dfs(回溯+剪枝);

2.怎么保证找到的解是字典序最小呢?

让我们看上面的图;可先dfs字典序靠前的能到的点,当找到答案后不再向下寻找;

3.如何保证找到的答案一定可行

用数组记录是否遍历过,如已遍历过就不再考虑;

代码

C++
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;
int v,n,m,mapx[110],mapy[110],dis[9][2]={{0,0},{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
bool vis[100][110],flag;
bool ok(int x,int y)
{
	if(x<1||y<1||x>m||y>n)return false;
	if(vis[x][y]==true)return false;
	return true;
}
void dfs(int x,int y,int dep)
{
	if(flag)return;//剪枝;
	mapx[dep]=x;
	mapy[dep]=y;
	if(dep==m*n)//找到字典序最小正解;
	{
		flag=true;return;
	}

	for(int i=1;i<=8;i++)
		if(ok(x+dis[i][0],y+dis[i][1])==true)
		{
			vis[x+dis[i][0]][y+dis[i][1]]=true;
			dfs(x+dis[i][0],y+dis[i][1],dep+1);
			vis[x+dis[i][0]][y+dis[i][1]]=false;//回溯;
		}

}
int main()
{
	cin>>v;
	for(int k=1;k<=v;k++)
	{
		cout<<"Scenario #"<<k<<":"<<endl;
		flag=false;//判断是否已得出正解;
		memset(vis,0,sizeof(vis));
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=n;j++)//枚举起点;
			{
				vis[i][j]=true;//判断点是否走过;
				dfs(i,j,1);
				vis[i][j]=false;//回溯;
				if(flag==true)break;//找到答案跳出;
			}
			if(flag==true)break;//找到答案跳出;
		}
		if(flag==true)
			for(int i=1;i<=n*m;i++)
				printf("%c%d",mapx[i]+64,mapy[i]);
		else cout<<"impossible";
		cout<<endl<<endl;//中间空一行;
	}
	return 0;
}
原文