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

Description

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

按照字典序输出路径,方向要按照字典序来搜索。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
bool v[][];
int p, q;
int dir[][] = {{-,-},{-,},{-,-},{-,},
{,-},{,},{,-},{,}};
int px[], py[];
int step, flag;
char R[] = {'A','B','C','D','E','F','G','H'}; int dfs(int x, int y, int step)
{
if (step == p*q) {
flag = ;
for (int i = ; i < p*q; i++) {
printf("%c%d", R[px[i]],py[i]+);
}
printf("\n\n");
return ;
} int nx, ny;
for (int i = ; i < ; i++) {
nx = x + dir[i][];
ny = y + dir[i][];
if (!v[nx][ny] && nx>= && nx<q && ny>= && ny<p) {
v[nx][ny] = ;
px[step] = nx; py[step] = ny;
dfs(nx, ny, step+);
if (flag) return ; //只搜索一次
v[nx][ny] = ;
}
}
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int T;
int t = ;
cin >> T;
while (T--) {
cin >> p >> q;
printf("Scenario #%d:\n", ++t);
memset(v, , sizeof(v));
px[] = ; py[] = ;
v[][] = ;
flag = ;
step = ;
if(!dfs(, , ))
printf("impossible\n\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(DFS)

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

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

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

  4. 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 ...

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

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

  6. POJ 2488 A Knight's Journey (回溯法 | DFS)

    题目链接:http://poj.org/problem?id=2488 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p ...

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

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

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

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

  9. 搜索 || DFS || POJ 2488 A Knight's Journey

    给一个矩形棋盘,每次走日字,问能否不重复的走完棋盘的每个点,并将路径按字典序输出 *解法:按字典序输出路径,因此方向向量的数组按字典序写顺序,dfs+回溯,注意flag退出递归的判断,并且用pre记录 ...

随机推荐

  1. 题解 P4799 【[CEOI2015 Day2]世界冰球锦标赛】

    题解 P4799 [[CEOI2015 Day2]世界冰球锦标赛] 双向搜索好题 传送门 实际上,双向搜索就是把\(a^n\)的复杂度转变成了大多为\(O(nlogna^{\frac{n}{2}})\ ...

  2. Linux入门基础(一)——ls、mkdir命令

  3. Machine Learning No.9: Dimensionality reduction

    1. Principal component analysis algorithm data preprocessing 2. choosing the number of principal com ...

  4. NPM 与 Nodejs

    安装了Nodejs之后,NPM也安装好了 如何知道当前是否已经安装Nodejs和NPM了呢? node -v //查看当前nodejs的版本 npm -v //查看当前npm的版本 NPM 初始化 n ...

  5. C语言实现队列(纯C)

    1. [代码][C/C++]代码 #include <stdio.h>#include <stdlib.h>#define ElemType int #define Statu ...

  6. 【详解】苹果AppStore审核被拒,原因终逃不过这些!

    近日,相信很多开发者都留意到了: 苹果针对应用标题的审核确有明显的变严趋势!我们在<惊!苹果再次加强审核力度,众App纷纷止步应用标题>中也对该现象进行了详细的分析,并给出了相应的解决方案 ...

  7. JS事件派发器EventEmitter

    原文地址:http://zhangyiheng.com/blog/articles/js_event_mitter.html 需求 随着Browser客户端JS越来越复杂,MVC(Client端)设计 ...

  8. python基础-正则1

    什么是正则表达式? 正则表达式是一种小型的\高度专业化的变成语言,主要用于字符串处理 正则表达式是一种通用语言,在python中通过re模块实现,import re 工具:在线正则表达式测试 http ...

  9. 多线程mtr-代码

    #!/bin/env python # -*- coding: utf-8 -*- # @Date : 2015-09-06 11:30:48 # @Author : Your Name (you@e ...

  10. visual studio code使用MSVC编译C++

    环境 OS::Microsoft Windows [Version 10.0.17134.285] x64 VSC:Version:1.27.2 (system setup) VS:2017 心血来潮 ...