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 简述:给你一副p*q的棋盘,求出每个点恰好只走一次的路径,若有多个答案输出字典序最小的。
分析:求最深路径,只经过一次,DFS+回溯,注意到如果其能满足题意,那么必然会经过A1,从A1开始字典序最小,所以从A1开始DFS搜索即可,注意保证dx与dy也是字典序排序,代码如下:
const int maxm = ;
//注意字典序大小排序
const int dx[] = {-, , -, , -, , -, };
const int dy[] = {-, -, -, -, , , , }; int vis[maxm][maxm], Next[maxm][maxm], r, c, n, kase = ; bool inside(int x,int y) {
return x > && x <= r && y > && y <= c;
} void print(int x,int y,int t) {
if(t) {
printf("%c%d", y - + 'A', x);
print(Next[x][y] / , Next[x][y] % , t - );
}
} bool dfs(int x,int y,int t) {
vis[x][y] = ;
if(t == r * c) {
return true;
}
for (int i = ; i < ; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if(inside(nx,ny) && !vis[nx][ny]) {
Next[x][y] = nx * + ny;
if(dfs(nx, ny,t+)) {
return true;
}
}
}
vis[x][y] = ;
return false;
} int main() {
scanf("%d", &n);
while(n--) {
scanf("%d%d", &r, &c);
memset(vis, , sizeof(vis)), memset(Next, , sizeof(Next));
printf("Scenario #%d:\n", ++kase);
if(dfs(, , )) {
print(,,r*c);
} else {
printf("impossible");
}
printf("\n\n");
}
return ;
}

Day2-F-A Knight's Journey POJ-2488的更多相关文章

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

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

  2. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

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

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

  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(深搜+回溯)

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

  6. POJ 2488 A Knight&#39;s Journey

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

  7. POJ 2488:A Knight's Journey 深搜入门之走马观花

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

  8. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

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

  9. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  10. POJ2488A Knight's Journey[DFS]

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

随机推荐

  1. 「题解」Just A String

    目录 题目 原题目 简易题意 思路及分析 代码 题目 原题目 点这里 简易题意 现定义一个合法的字符串满足将其打散并任意组合之后能够形成回文串. 给你 \(m\) 种字母,问随机构成长度为 \(n\) ...

  2. Deepin-linux下的linux的终端下软件安装和卸载方法

    1.方法一: sudo apt update #最好第一步是它 sudo apt install <package_name> --no-upgrade #安装该package但是不升级. ...

  3. 对于java中反编译命令的使用以及Integer包装类的查看

    Integer是基于int的包装类 我们可以用测试代码来看看Integer类是如何实现装箱和拆箱的 public class BoxAndUnbox { /** * @param args */ pu ...

  4. 安装pytorch

    安装cpu版本的 conda install pytorch-cpu torchvision-cpu -c pytorch 安装gpu版本的 conda install pytorch torchvi ...

  5. 【SSM】日志框架 logback

    logback.xml <?xml version="1.0" encoding="UTF-8" ?> <configuration scan ...

  6. BGR 与 HSV 模式的转换规则

    HSV模式中的H.S.V分别表示色调.饱和度.亮度 RGB转化到HSV的算法:max=max(R,G,B) min=min(R,G,B) if R = max, H = (G-B)/(max-min) ...

  7. 「AT4741 [ABC132D] Blue and Red Balls」

    题目大意 给出一个长度为 \(N\) 的01串,其中有 \(K\) 个 \(1\),其他都是 \(0\),需要求出当着 \(K\) 个 \(1\) 分成 \(1\) 到 \(K\) 段每一个的方案数. ...

  8. 关于MQTT连接的属性

    连接相关的属性. 这些属性是MQTT的连接报文中连接标志字, 包含一些用于指定 MQTT 连接行为的参数. 1.清理会话(Clean Session) 客户端和服务端可以保存会话状态,以支持跨网络连接 ...

  9. ios APP进程杀死之后和APP在后台接收到推送点击跳转到任意界面处理

    https://www.jianshu.com/p/ce0dc53eb627 https://www.cnblogs.com/er-dai-ma-nong/p/5584724.html github: ...

  10. 牛顿迭代法--求任意数的开n次方

    牛顿迭代法是求开n次方近似解的一种方法,本文参考. 引言 假如\(x^n = m\),我们需要求x的近似值. 我们设\(f(x) = x^n - m\), 那么也就是求该函数f(x)=0时与x轴的交点 ...