题目链接: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. DataBase -- Employees Earning More Than Their Managers My Submissions Question

    Question: The Employee table holds all employees including their managers. Every employee has an Id, ...

  2. [USACO06NOV]玉米田Corn Fields

    题面描述 状压dp. 设\(f[i][sta]\)为第\(i\)层状态为\(sta\)的方案数. 然后每次可以枚举上一层的状态以及本层的状态,然后如果不冲突且满足地图的要求,则转移. 时间复杂度\(O ...

  3. [洛谷P2568]GCD

    题目大意:给你$n(1\leqslant n\leqslant 10^7)$,求$\displaystyle\sum\limits_{x=1}^n\displaystyle\sum\limits_{y ...

  4. [Leetcode] Populating next right pointer in each node 填充每个节点的右指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  5. Maven如何打包本地依赖包

    有的jar包,在maven中心库里面是没有的,那么,如何在项目中使用呢? 假设我们需要使用:apache-ant-zip-2.3.jar 将该jar包,放在项目的lib目录,例如: 在pom.xml里 ...

  6. Reasons to use innodb_file_per_table

    When working with InnoDB, you have two ways for managing the tablespace storage: Throw everything in ...

  7. Virtualization solutions on Linux systems - KVM and VirtualBox

    Introduction Virtualization packages are means for users to run various operating systems without &q ...

  8. 关于flume的几道题

    1,要求:监听一个tcp,udp端口41414将数据打印在控制台 # example.conf: A single-node Flume configuration # Name the compon ...

  9. java基础学习(一)hashcode

    hashcode的作用 hashCode()方法是从Object类继承过来的,Object类中的hashCode()方法返回的是对象在内存中地址转换成的int值,如果对象没有重写hashCode()方 ...

  10. 【BZOJ2039】【2009国家集训队】人员雇佣 [最小割]

    人员雇佣 Time Limit: 20 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description 作为一个富有经营头脑的富翁,小L决 ...