POJ2488:A Knight's Journey

点击打开题目链接


A Knight's Journey

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25877 Accepted: 8829

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? 

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


=====================================题目大意=====================================


输出国际象棋中马遍历指定规格棋盘的字典序下的第一条路径(路径中每个格子由一个大写字母后跟一个数字来描述)。


=====================================算法分析=====================================


我在网上的题解和POJ的[Discuss]中发现不少人都认为题意遗漏,没有说字母代表的是列而数字代表的是行。

事实上只要仔细看题就会知道谁作为列谁作为行是无所谓的。

但在这里考虑到国际象棋棋盘的实际情况,我也就用字母代表列而数字代表行了。

算法明显是用DFS+回溯,只是由于要求输出的路径是字典序下的第一条,所以需要处理以下两点:

一、DFS起点的枚举需要首先尽量使列(也就是字母)小,其次尽量使行(也就是数字)小。

二、DFS的方向数组需要首先尽量降低列(也就是字母)的大小,其次尽量降低行(也就是数字)的大小。


关于第二点有个神问题:只考虑以左上角(也就是第1行第A列)为起点的路径也能AC。

我在网上的一些题解中也发现了这样的做法,但是它们都没有给出这么做的理论依据。

考虑到本题的本质是哈密顿通路(注意是通路不是回路),我稍微看了点此方面的资料,也没有发现支持这么做的定理。

只有留着以后再仔细斟酌了。下面顺便附上探究该问题用的测试代码。


=======================================代码=======================================


一、AC代码。




#include<stdio.h>        

#define LEGCORD(COL,ROW) ((0<=COL&&COL<Q)&&(0<=ROW&&ROW<P))

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

int T,P,Q,PathLen,SavePath[30][2];

bool HashSquare[30][30],GetAns;

void DFS(int Col,int Row)
{
	SavePath[PathLen][0]=Col;
	SavePath[PathLen][1]=Row;
	++PathLen;
	HashSquare[Row][Col]=true;
	if(PathLen==P*Q)
	{
		GetAns=true;
		for(int i=0;i<PathLen;++i)
		{
			printf("%c%d",SavePath[i][0]+'A',SavePath[i][1]+1);   
		}
		printf("\n");
	}
	else
	{
		for(int n=0;n<8;++n) 
		{
			int tmpcol=Col+Dir[n][0];
			int tmprow=Row+Dir[n][1];
			if(LEGCORD(tmpcol,tmprow)&&!HashSquare[tmprow][tmpcol])
			{
				DFS(tmpcol,tmprow);
				if(GetAns) { break; }
			}
		}	
	}
	--PathLen;
	HashSquare[Row][Col]=false;
}

int main()
{
	while(scanf("%d",&T)==1) 
	{
		for(int cas=1;cas<=T;++cas)
		{
			scanf("%d%d",&P,&Q);
			GetAns=0;
			printf("Scenario #%d:\n",cas);

			/*正常应该是这么做↓
			for(int col=0;col<Q&&!GetAns;++col)
			{
				for(int row=0;row<P&&!GetAns;++row)
				{
					DFS(col,row);
				}
			}
			但这么做也能通过↓*/
			DFS(0,0);

			if(!GetAns)  { printf("impossible\n"); } 
			if(cas+1<=T) { printf("\n"); }
		}
	}
	return 0;
}


二、测试代码。


#include<stdio.h>        

#define LEGCORD(COL,ROW) ((0<=COL&&COL<Q)&&(0<=ROW&&ROW<P))

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

int T,P,Q,PathLen,SavePath[30][2];

bool HashSquare[30][30],GetAns;

void DFS(int Col,int Row)
{
	SavePath[PathLen][0]=Col;
	SavePath[PathLen][1]=Row;
	++PathLen;
	HashSquare[Row][Col]=true;
	if(PathLen==P*Q)
	{
		GetAns=true;
		for(int i=0;i<PathLen;++i)
		{
			printf("%c%d",SavePath[i][0]+'A',SavePath[i][1]+1);
			if(i+1<PathLen) { printf(" "); } 
		}
		printf("\n");
	}
	else
	{
		for(int n=0;n<8;++n) 
		{
			int tmpcol=Col+Dir[n][0];
			int tmprow=Row+Dir[n][1];
			if(LEGCORD(tmpcol,tmprow)&&!HashSquare[tmprow][tmpcol])
			{
				DFS(tmpcol,tmprow);
				if(GetAns) { break; }
			}
		}	
	}
	--PathLen;
	HashSquare[Row][Col]=false;
}

int main()
{
	while(scanf("%d%d",&P,&Q)==2)
	{
		printf("------------------[ %d X %d 棋盘测试 ]------------------\n\n",P,Q);

		for(int col=0;col<Q;++col)
		{
			for(int row=0;row<P;++row)
			{
				printf("( %c , %d ) : ",col+'A',row+1); 
				GetAns=0;
				DFS(col,row);
				if(!GetAns) { printf("impossible\n"); }
			}
		}
		printf("\n--------------------------------------------------------\n\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值