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. 【题解】HNOI2013比赛

    [题解][P3230 HNOI2013]比赛 将得分的序列化成样例给的那种表格,发现一行和一列是同时确定的.这个表格之前是正方形的,后来长宽都减去一,还是正方形.问题形式是递归的.这就启示我们可以把这 ...

  2. linux install beanstalkd

    you can instal it via git and then copy systemd script: Step 0. Install git yum install git Step 1. ...

  3. angularJs自定义模块

    <script type="text/javascript"> var myApp = angular.module("myApp",[]); my ...

  4. LeetCode:访问所有节点的最短路径【847】

    LeetCode:访问所有节点的最短路径[847] 题目描述 给出 graph 为有 N 个节点(编号为 0, 1, 2, ..., N-1)的无向连通图. graph.length = N,且只有节 ...

  5. Wannafly挑战赛12 A 银行存款 【DP】【DFS】

    链接:https://www.nowcoder.com/acm/contest/79/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  6. hihocoder #1152 Lucky Substrings 【字符串处理问题】strsub()函数+set集合去重

    #1152 : Lucky Substrings时间限制:10000ms单点时限:1000ms内存限制:256MB描述A string s is LUCKY if and only if the nu ...

  7. POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串

    题目链接:https://vjudge.net/problem/POJ-2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072 ...

  8. 9.1 NOIP普及组试题精解(3)

    9-6 seat.c #include <stdio.h> #define MAXN 1001 void swap(int *a, int *b) //交换数据 { int t; t = ...

  9. Python成长之路第一篇(2)__初识列表和元组

    可以将列表和元组当成普通的“数组”,他能保存任意数量任意类型的Python对象,和数组一样都是通过数字0索引访问元素,列表和元组可以存储不同类型的对象,列表和元组有几处重要区别.列表元素用([])包括 ...

  10. jQuery蓝色修边tab标签切换

    jQuery蓝色修边tab标签切换,jQuery,tab选项卡,标签切换,jQuery蓝色修边tab标签广告代码切换是一款非常简单实用tab选项卡切换效果,自己定义好相关的html标签即可,选项卡切换 ...