POJ 2488 A Knight's Journey

/*
                                               A Knight's Journey
Time Limit: 1000MS  Memory Limit: 65536K 

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

*/

/*
	本题为POJ2488 ,之前A过一次所以再写一遍只用了1个小时左右
	大概意思为棋盘上的马走日..
	能遍历棋盘就输出路线
	用到了DFS回溯的思想,注意输出是字典序输出,也就是每走一步都从左下角开始优先遍历
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int dicy[8] = {-2,-2,-1,-1,1,1,2,2};    
int dicx[8] = {-1,1,-2,2,-2,2,-1,1};    //字典树排序 注意优先顺序
int bingo = 0;
int visit[100][100];
int trackx[200],tracky[200];
int x,y;
int length;

int ok(int n,int m)                    //判断是否在棋盘界内
{
	if(n <= x && n >= 1 && m >= 1 && m <= y)
		return 1;
	else 
		return 0;
}

void dfs(int m,int n,int length)      //深搜
{
	trackx[length] =  m;
	tracky[length] =  n;
	if(length == x*y)
	{
		bingo = 1;
		for(int j = 1 ; j <= length ; j++)            //输出
		{
			printf("%c%d",tracky[j]+'A'-1,trackx[j]);
		}
		printf("\n\n");
	}
	for(int i = 0 ; i < 8 ; i++)
	{
		if(ok(m+dicx[i],n+dicy[i]) && !visit[m+dicx[i]][n+dicy[i]] && !bingo)
		{
			visit[m+dicx[i]][n+dicy[i]] = 1;
			dfs(m+dicx[i],n+dicy[i],length+1);
			//visit[m+dicx[i]][n+dicy[i]] = 0;    //回溯   
		}
	}
}

int main()
{
	
	int n;
	scanf("%d",&n);
	for(int k = 1 ; k <= n ; k++)
	{
		bingo = 0;
		for(int m = 0 ; m < 20 ; m++)
		{
			for(int n = 0 ; n < 20 ; n++)
			{
				visit[m][n] = 0 ;
			}
		}
		scanf("%d%d",&x,&y);   // 输入x,y       x为列数,y为行数
		trackx[1] = 1;
		tracky[1] = 1;
		visit[1][1] = 1;
		printf("Scenario #%d:\n",k);
		dfs(1,1,1);
		if(!bingo)
		{
			printf("impossible\n\n");
		}
		
	}
	return 0;
}

 

posted @ 2012-03-10 20:09  Felix_F  阅读(121)  评论(0编辑  收藏  举报