题目链接:http://poj.org/problem?id=2488

A Knight's Journey
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 36695   Accepted: 12462

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

题目大意: 任选一个起点,按照国际象棋马的跳法,不重复的跳完整个棋盘,如果有多种路线则选择字典序最小的路线(路线是点的横纵坐标的集合,注意棋盘的横坐标的用大写字母,纵坐标是数字)

题目分析: 

1. 应该看到这个题就可以想到用DFS,当首先要明白这个题的意思是能否只走一遍(不回头不重复)将整个地图走完,而普通的深度优先搜索是一直走,走不通之后沿路返回到某处继续深搜。所以这个题要用到的回溯思想,如果不重复走一遍就走完了,做一个标记,算法停止;否则在某种DFS下走到某一步时按马跳的规则无路可走而棋盘还有为走到的点,这样我们就需要撤消这一步,进而尝试其他的路线(当然其他的路线也可能导致撤销),而所谓撤销这一步就是在递归深搜返回时重置该点,以便在当前路线走一遍行不通换另一种路线时,该点的状态是未访问过的,而不是像普通的DFS当作已经访问了。

2. 如果有多种方式可以不重复走一遍的走完,需要输出按字典序最小的路径,而注意到国际象棋的棋盘是列为字母,行为数字,如果能够不回头走一遍的走完,一定会经过A1点,所以我们应该从A1开始搜索,以确保之后得到的路径字典序是最小的(也就是说如果路径不以A1开始,该路径一定不是字典序最小路径),而且我们应该确保优先选择的方向是字典序最小的方向,这样我们最先得到的路径就是字典序最小的。

参考代码:

#include <cstdio>
#include <cstring> using namespace std; const int MAX_N = ;
//字典序最小的行走方向
const int dx[] = {-, , -, , -, , -, };
const int dy[] = {-, -, -, -, , , , };
bool visited[MAX_N][MAX_N];
struct Step{
char x, y;
} path[MAX_N];
bool success; //是否成功遍历的标记
int cases, p, q; void DFS(int x, int y, int num); int main()
{
scanf("%d", &cases);
for (int c = ; c <= cases; c++)
{
success = false;
scanf("%d%d", &p, &q);
memset(visited, false, sizeof(visited));
visited[][] = true; //起点
DFS(, , );
printf("Scenario #%d:\n", c);
if (success)
{
for (int i = ; i <= p * q; i++)
printf("%c%c", path[i].y, path[i].x);
printf("\n");
}
else
printf("impossible\n");
if (c != cases)
printf("\n"); //注意该题的换行
}
return ;
} void DFS(int x, int y, int num)
{
path[num].y = y + 'A' - ; //int 转为 char
path[num].x = x + '';
if (num == p * q)
{
success = true;
return;
}
for (int i = ; i < ; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if ( < nx && nx <= p && < ny && ny <= q
&& !visited[nx][ny] && !success)
{
visited[nx][ny] = true;
DFS(nx, ny, num+);
visited[nx][ny] = false; //撤销该步
}
}
}

 

POJ2488-A Knight's Journey(DFS+回溯)的更多相关文章

  1. POJ2488:A Knight's Journey(dfs)

    http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...

  2. poj2488 A Knight's Journey裸dfs

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35868   Accepted: 12 ...

  3. POJ2488A Knight's Journey[DFS]

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41936   Accepted: 14 ...

  4. 迷宫问题bfs, A Knight's Journey(dfs)

    迷宫问题(bfs) POJ - 3984   #include <iostream> #include <queue> #include <stack> #incl ...

  5. 快速切题 poj2488 A Knight's Journey

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31195   Accepted: 10 ...

  6. POJ2488 A Knight's Journey

    题目:http://poj.org/problem?id=2488 题目大意:可以从任意点开始,只要能走完棋盘所有点,并要求字典序最小,不可能的话就impossible: 思路:dfs+回溯,因为字典 ...

  7. A Knight's Journey(dfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25950   Accepted: 8853 Description Back ...

  8. [poj]2488 A Knight's Journey dfs+路径打印

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45941   Accepted: 15637 Description Bac ...

  9. poj-2488 a knight's journey(搜索题)

    Time limit1000 ms Memory limit65536 kB Background The knight is getting bored of seeing the same bla ...

  10. POJ2248 A Knight's Journey(DFS)

    题目链接. 题目大意: 给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点. 列为数字,行为字母,搜索按字典序. 分析: 用 vis[x][y] 标记是否已经访问.因为要搜索所 ...

随机推荐

  1. python中的__init__ 、__new__、__call__小结

    这篇文章主要介绍了python中的__init__ .__new__.__call__小结,需要的朋友可以参考下 1.__new__(cls, *args, **kwargs)  创建对象时调用,返回 ...

  2. web ppt

    先记录,以后再试试 https://github.com/gnab/remark/wiki http://segmentfault.com/blog/sweetdum/1190000000484121 ...

  3. PowerDesigner工具箱(palette)如何打开

    我使用的PowerDesigner是15.1版本的,其他版本的操作可能会有所不同 我们在使用PowerDesigner的时候,有时候可能会不小心把悬浮的工具箱隐藏掉,就是下面这个东西 怎么显示出来呢, ...

  4. 1、Flat UI Getting started(文档翻译)

    下载链接:http://www.bootcss.com/p/flat-ui/ 一.什么是Flat UI? Flat UI 是一种漂亮的Boostrap主题.我们重新设计了它的很多组件,使得其看起来扁平 ...

  5. css清除浮动定位造成的异常

    清除浮动是为了解决高度塌陷的问题:内层有好几个div有宽有高,并且选择了浮动定位,但是外层的div却并没有设置宽高.在非IE浏览器(如Firefox)下,当容器的高度为auto,且容器的内容中有浮动( ...

  6. C#进制转换

    //十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Console.WriteLine(Convert.ToString(6 ...

  7. 利用Android多进程机制来分割组件

    android对于内存有一定的限制,很多手机上对内存的限制是完全不同的.我们的应用程序其实就是一个进程,这个进程是完全独立的,这个进程分配的内存是一定的,所以我们经常会遇到OOM的问题.但,你可能不知 ...

  8. iOS支付宝集成详细流程

    实现支付宝支付的准备工作: 1.向支付宝签约,成为支付宝的商户 签约完成后,支付宝会提供一些必要的数据给我们 商户ID:partner 账号ID:seller 即支付宝账号 签约需要营业执照 2.获取 ...

  9. Android开发笔记——常见BUG类型之内存泄露与线程安全

    本文内容来源于最近一次内部分享的总结,没来得及详细整理,见谅. 本次分享主要对内存泄露和线程安全这两个问题进行一些说明,内部代码扫描发现的BUG大致分为四类:1)空指针:2)除0:3)内存.资源泄露: ...

  10. 关于C语言中单双引号的问题

    代码 #include<stdio.h> int main() { if ( "{" =='{' ) printf("True\n"); else ...