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

题意:

  在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p * q的矩形格子,让你找一个跳跃顺序(起点自选),使得这个顺序恰好经过矩阵的每一个格子,且每一个格子仅经过一次,即找一个符合跳跃条件的序列,遍历整个矩形格子。如果有多个,那么就输出字典序最小的。

思路:

  貌似可以利用哈密顿通路来解决,但是感觉有点太麻烦,没怎么细想,感觉还是回溯法比较好。首先题目要求字典须,所以拓展节点的顺序不能是随意的,这里把图抽象一下:

A  B C  D  E  F  G

1| *  *  3  *  5  *  *

2| *  1  *  *  *  7  *

3| *  *  *  @ *  *  *

4| *  2  *  *  *  8  *

5| *  *  4  *  6  *  *

假设“@”为骑士某时某刻的位置,可以想到先考虑位置“1”为字典需最小的,紧接着2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8为字典序依次增大的选择,所以每次的选择必须按照上面路径才可保证第一个找到的为字典需最大的。由于这道题的特殊性,可以直接以“A1”为起点,搜索路径,而不用枚举起点进行搜索,大概是因为题目给的所有数据都有以“A1”为起点的路径吧。

代码:

 #include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <string>
#define memst(a, b) memset((a), (b), sizeof((a))) typedef long long LL;
using namespace std;
const int MAXN = ;
int map[MAXN + ][MAXN + ];
int stepX[] = {-, , -, , -, , -, };//字典序依次增大的选择
int stepY[] = {-, -, -, -, , , , };
int ok;
int p, q; typedef struct Chess{
char ch;//字母
int nu;//数字
}chess;
chess ve[MAXN + ]; int check(int x, int y) {//剪掉不可能的搜索子树
if(x <= || y <= || x > p || y > q) return ;
if(map[x][y]) return -;
return ;
} void backtrack(int x, int y, int n) {
if(ok) return ;
if(n == p * q) {//找到了一条路经
chess tp;
tp.ch = y + 'A' - , tp.nu = x;
ve[n] = tp;
ok = ;
return ;
}
else {
for(int i = ; i < ; i++) {//往周围八个方向进行试探
int nex = x + stepX[i], ney = y + stepY[i];
chess tp;
tp.ch = y + 'A' - , tp.nu = x;
ve[n] = tp;
if(check(nex, ney) > ) {//剪枝
map[nex][ney] = ;
backtrack(nex, ney, n + );
map[nex][ney] = ; //恢复现场
}
}
}
} int main() {
int T;
scanf("%d", &T);
int kas = ;
while(T--) {
scanf("%d%d", &p, &q);
memset(map, , sizeof(map));
ok = ;
int stX = , stY = ;//起点
map[stX][stY] = ;
memset(&ve, , sizeof(chess));
backtrack(stX, stY, );
printf("Scenario #%d:\n", kas++);
if(ok) {
for(int i = ; i <= p * q; i++) {
printf("%c%d", ve[i].ch, ve[i].nu);
}
printf("\n");
}
else printf("impossible\n");
if (T)printf("\n");
}
return ;
}

  

POJ 2488 A Knight's Journey (回溯法 | DFS)的更多相关文章

  1. POJ 2488 -- A Knight's Journey(骑士游历)

    POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...

  2. POJ 2488 A Knight's Journey(深搜+回溯)

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  3. Poj 2488 A Knight's Journey(搜索)

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  4. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...

  5. poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】

    题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...

  6. poj 2488 A Knight's Journey( dfs )

    题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...

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

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

  8. POJ 2488 A Knight's Journey【DFS】

    补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...

  9. POJ 2488 A Knight's Journey (DFS)

    poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...

随机推荐

  1. Codeforces 821E Okabe and El Psy Kongroo(矩阵快速幂)

    E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. 如何用setInterval调用类的方法

    setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式.setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭.由 se ...

  3. C#语法糖大汇总【转发】

    首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1.  ...

  4. POJ - 1017 贪心训练

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59725   Accepted: 20273 Descrip ...

  5. 使用命令wsimport生成WebService客户端

    使用命令wsimport生成WebService客户端 wsimpost命令有几个重要的参数: -keep:是否生成java源文件    -d:指定输出目录    -s:指定源代码输出目录    -p ...

  6. HUSTOJ增加其他语言出现RuntimeError解决办法

    HUSTOJ增加其他语言,如Python.Java.Pascal等等,如果程序是正确的,却报运行错误,添加okcall就行. 具体错误可以看日志: [ERROR] A Not allowed syst ...

  7. webpack 配置学习笔记

    最简单的 webpack 配置 const path = require('path') module.exports = { entry: './app/index.js', output: { p ...

  8. Python 进阶学习笔记

    把函数作为参数 import math def add(x, y, f): return f(x) + f(y) print add(, , math.sqrt) map(f, list) 函数 接收 ...

  9. css做中划线与文字排版

    html: <div class="spilt">    <span class="left"></span>    < ...

  10. bzoj1861 书架 splay版

    单点插入删除以及求前缀 #include<cstdio> #include<cstring> #include<algorithm> using namespace ...