POJ 2488 A Knight's Journey (回溯法 | DFS)
题目链接: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)的更多相关文章
- POJ 2488 -- A Knight's Journey(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- POJ 2488 A Knight's Journey(深搜+回溯)
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- Poj 2488 A Knight's Journey(搜索)
Background The knight is getting bored of seeing the same black and white squares again and again an ...
- POJ 2488 A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- 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 ...
- poj 2488 A Knight's Journey( dfs )
题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- POJ 2488 A Knight's Journey【DFS】
补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...
- POJ 2488 A Knight's Journey (DFS)
poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...
随机推荐
- BZOJ4415 SHOI2013发牌(线段树)
似乎是noip2017d2t3的一个部分分.用splay的话当然非常裸,但说不定会被卡常.可以发现序列中数的(环上)相对位置是不变的,考虑造一棵权值线段树维护权值区间内还有多少个数留在序列中,每次在线 ...
- 【题解】ZJOI2010贪吃的老鼠
%%%%真的好强...看题解我都看了好久才完全明白.放一下参考的博客,谢谢神犇QAQ 1号博客 2号博客(超级赞的啦) 因为理解的过程太艰辛,所以必须记录一下这道强题:这道题目最难的两个约束就在 ...
- 淡入淡出效果的js原生实现
淡入淡出效果,在日常项目中经常用到,可惜原生JS没有类似的方法,而有时小的页面并不值得引入一个jQuery库,所以就自己写了一个,已封装, 有用得着的朋友, 可以直接使用. 代码中另附有一个设置元素透 ...
- Codeforces Round #525 (Div. 2)B. Ehab and subtraction
B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...
- HDU 多校对抗赛 D Distinct Values
Distinct Values Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- C++ 中 string, char*, int 类型的相互转换
一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...
- 生产服务器环境最小化安装后Centos 6.5优化配置备忘
生产服务器环境最小化安装后 Centos 6.5优化配置备忘 本文 centos 6.5 优化 的项有18处,列表如下: 1.centos6.5最小化安装后启动网卡 2.ifconfig查询IP进行S ...
- js操作div的显隐
<!DOCTYPE html><html> <head> <title> new document </title> <meta ht ...
- 着色方案(bzoj 1079)
Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木 ...
- vivo面试经验4(linux基本操作,最基本,必须得会!!)
操作linux通过xshell进行连接: 基本操作介绍 1.shutdown -h 关机 shutdown -r 重启 2.mkdir aaa 新建目录aaa rmdir aaa 删除目录aaa 3. ...