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. hexo 搭建静态博客 + Next 主题配置

    参考手册 HEXO:https://hexo.io/zh-cn/ NEXT:http://theme-next.iissnan.com/ 安装hexo npm install hexo-cli -g ...

  2. java获取当前机器的公网ip

    package com.Interface.util; import javax.servlet.http.HttpServletRequest; /** * 测试类 * * @author 华文 * ...

  3. 深度学习之父低调开源 CapsNet,欲取代 CNN

    “卷积神经网络(CNN)的时代已经过去了!” ——Geoffrey Hinton 酝酿许久,深度学习之父Geoffrey Hinton在10月份发表了备受瞩目的Capsule Networks(Cap ...

  4. 【Html 页面布局】

    float:left方式布局 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /& ...

  5. 李彦宏AI大会现场:3秒钟事故30分钟专注

    编辑 | 于斌 出品 | 于见(mpyujian) 很多人只看到了舞台上3秒钟的事故,却没有看到李彦宏在台上30分钟的专注. 7月3号,百度AI开发者大会上,李彦宏遭遇了3秒钟的突然袭击,他表现的沉着 ...

  6. Linux批量装机(PXE)!

    一 .PXE 简介PXE:Pre-boot Excution Environment,预启动执行环境PXE 是由 Intel 公司开发的网络引导技术,工作在 Client/Server 模式,允许客户 ...

  7. knockout 简单使用

    定义: var QcViewModel = function () { var self = this; self.name = ko.observable(); self.qty = ko.obse ...

  8. vue 之 axios Vue路由与element-UI

    一. 在组件中使用axios获取数据 1. 安装和配置axios 默认情况下,我们的项目中并没有对axios包的支持,所以我们需要下载安装. 在项目根目录中使用 npm安装包 npm install ...

  9. 2019最新整理JAVA面试题附答案

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  10. 五年C语言程序员,是深耕技术还是走管理?

    从进入程序员行列开始(2013年6月),到现在为止(2019年2月),已经有五年半了.    一路波折,已经从无知菜鸟走到了意识觉醒的老鸟了.    薪资变化情况如下: 2013年:2000元/月 ( ...