POJ-2488 A Knight's Journey(需注意搜索顺序的深搜)

原创 2013年12月05日 12:09:52
A Knight's Journey
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26950   Accepted: 9195

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

思路:
         深度优先搜索,但因为最后要按字典序输出走过的路径,所以需要注意搜索方向的顺序问题;
         

代码:

#include <stdio.h>
#include <string.h>
#include <stack>
#define N 30

using namespace std;

struct Node{
	int x;
	int y;
};

int m, n, count;
char map[N][N];
int fx[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
stack<Node>q;

int dfs(Node cur);

int main()
{
	int k, t = 0;
	scanf("%d", &k);
	while(k --){
		t ++;
		while(!q.empty()) q.pop();
		scanf("%d%d", &n, &m);
		memset(map, '-', sizeof(map));
		for(int i = 1; i <= m; i ++){
			for(int j = 1; j <= n; j ++){
				map[i][j] = '0';
			}
		}
		count = 1;
		Node start;
		start.x = 1;
		start.y = 1;
		map[1][1] = '-';
		int ok = dfs(start);
		printf("Scenario #%d:\n", t);
		if(!ok) printf("impossible");
		else{
			q.push(start);
			while(!q.empty()){
				printf("%c%d", q.top().x + 'A' - 1, q.top().y);
				q.pop();
			}
		}
		printf("\n\n");
	}

	return 0;
}

int dfs(Node cur)
{
	if(count == m * n) return 1;
	for(int i = 0; i < 8; i ++){
		Node nw = cur;
		nw.x += fx[i][0];
		nw.y += fx[i][1];
		if(nw.x <= 0 || nw.y <= 0 || map[nw.x][nw.y] == '-') continue;
		else{
			count ++;
			map[nw.x][nw.y] = '-';
			int ok = dfs(nw);
			if(ok == 1){
				q.push(nw);
			   	return 1;
			}
			count --;
			map[nw.x][nw.y] = '0';
		}
	}

	return 0;
}


POJ 3984 迷宫问题 广搜迷宫解法

本题是经典的迷宫搜索问题了,使用广搜比使用深搜效率要高。 思路关键点: 1 从终点出发查找起点,这样方便记录路径 2 每次查找到下一个空格,可走方格之后,可以马上标识该格为不可走了 3 找到起点之后,...
  • kenden23
  • kenden23
  • 2014年10月29日 09:55
  • 2035

DFS深度优先搜索(2)--poj1321(棋盘问题 经典DFS)

棋盘问题  Time Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%lld & %llu Description...
  • Acmer_Sly
  • Acmer_Sly
  • 2016年09月10日 14:17
  • 1262

从排列到组合——深度优先搜索

这个核心代码不超过20行的例子,饱含深度优先搜索(Depth First Search,DFS)的基本模型。理解深度优先搜索的关键在于解决“当下该如何做”。至于“下一步如何做”则与“当下该如何做”是一...
  • yyyds
  • yyyds
  • 2016年06月19日 15:45
  • 1464

POJ-2488 A Knight's Journey(深搜DFS)

POJ-2488 A Knight’s JourneyA Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total S...
  • santa9527
  • santa9527
  • 2017年01月14日 09:02
  • 81

POJ 2488 A Knight's Journey (DFS深搜)

A - A Knight's Journey   123
  • sizaif
  • sizaif
  • 2017年07月24日 09:27
  • 87

POJ2488 A Knight's Journey(深搜DFS,字典序,骑士游历问题)

题目: A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4298...
  • riba2534
  • riba2534
  • 2017年01月07日 17:35
  • 639

POJ 2488 A Knight's Journey (dfs+改变搜索顺序)

题目链接POJ 2488题目大意给定一个n*m的棋盘,一个跳“日”字的马可以从任一个格子开始去遍历棋盘,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径。 棋盘的编号:n(行)方向...
  • yyt330507870
  • yyt330507870
  • 2017年02月06日 11:44
  • 169

poj2488 A Knight's Journey 简单DFS 注意搜索步骤

这一题是比较简单的搜索题,但是题目要求走法是按照字典序的,这样要求搜索时注意顺序。///2014.3.10 ///poj2488 //16MS /** *简单的DFS,但是题目有个坑人的地方,就...
  • u012278856
  • u012278856
  • 2014年05月11日 15:58
  • 443

poj 2488 A Knight's Journey(深度优先搜索)

     刚开始练习搜索,今天做这个破题,做了一天到晚上9点才给AC,做到最后我差点崩溃了,在过程中我遇到了N多问题,最主要的原因无非就是方向问题。以前没有怎么用过方向数组,做那种简单的深度搜索题也就...
  • tongyongzh
  • tongyongzh
  • 2011年01月06日 22:02
  • 699

POJ 2488 A Knight's Journey 水搜索

题目大意:给一些棋盘的大小,
  • jiangyuze831
  • jiangyuze831
  • 2014年09月01日 19:35
  • 546
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ-2488 A Knight's Journey(需注意搜索顺序的深搜)
举报原因:
原因补充:

(最多只允许输入30个字)