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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章