poj 2488 A Knight’s Journey

dfs 注意字典序输出

A Knight’s Journey
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27075 Accepted: 9231

Description

19225155_84Zs.jpg
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

[Submit]   [Go Back]   [Status]   [Discuss]

/*=============================================================================
#     FileName: 2488.cpp
#         Desc: poj 2488
#       Author: zhuting
#        Email: cnjs.zhuting@gmail.com
#     HomePage: my.oschina.net/locusxt
#      Version: 0.0.1
#    CreatTime: 2013-12-19 22:46:13
#   LastChange: 2013-12-19 22:46:13
#      History: dfs
=============================================================================*/
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#define maxn 66

int x_size = 0, y_size = 0;
int chess_num = 0;/*棋盘的格子数*/
int rout[maxn] = {0};/*路径*/

bool color[10][10] = {0};/*记录是否走过*/
int col_sum = 0;/*走过的点的总数*/


const int del_x[8] = {-2, -2, -1, -1, 1, 1, 2, 2};/*路径要字母序*/
const int del_y[8] = {-1, 1, -2, 2, -2, 2, -1, 1};

bool dfs(int x, int y)
{
	if (x < 0 || x >= x_size || y < 0 || y >= y_size)/*越界*/
		return 0;
	if (color[x][y])/*已经走过*/
		return 0;
	color[x][y] = 1;
	++col_sum;
	rout[col_sum - 1] = x * y_size + y;
	if (col_sum == chess_num)/*已经遍历全部*/
		return 1;
	for (int i = 0; i < 8; ++i)
	{
		bool is_pos = dfs(x + del_x[i], y + del_y[i]);
		if (is_pos) return 1;
	}
	--col_sum;
	color[x][y] = 0;
	return 0;
}

void init()
{
	memset(color, 0, sizeof(color));
	memset(rout, 0, sizeof(rout));
	chess_num = x_size * y_size;
	col_sum = 0;
}

int main()
{
	int test_num = 0;
	scanf("%d", &test_num);
	for (int i = 0; i < test_num; ++i)
	{
		printf("Scenario #%d:\n", i + 1);
		scanf("%d %d", &y_size, &x_size);/*写完发现搞反了*/
		init();
		if (dfs(0, 0))
		{
			for (int j = 0 ; j < chess_num; ++j)
			{
				printf("%c%d", rout[j] / y_size + 'A', rout[j] % y_size + 1);
			}
			printf("\n\n");
			continue;
		}
		printf("impossible\n\n");
	}
	return 0;
}

转载于:https://my.oschina.net/locusxt/blog/186107