Time limit1000 ms

Memory limit65536 kB

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 题意:骑士走棋盘,要求把所有的各自都要走一遍,并且要输出走棋盘的格子
题解:dfs搜索吧,注意每次可以搜索的时候都要把步数加一,当步数等于格子数时就可以了
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
using namespace std;
#define PI 3.14159265358979323846264338327950 int path[][],vis[][],p,q,cnt;
bool flag;
int dx[] = {-, , -, , -, , -, };
int dy[] = {-, -, -, -, , , , }; bool judge(int x,int y)
{
if(x<=p && x>= && y<=q && y>= && !vis[x][y] )
return true;
return false;
}
void dfs(int r,int c,int step)
{
if (flag == false)
{
path[step][]=r;
path[step][]=c;
}
if(step==p*q)
{
flag=true;
return ;
}
for(int i=;i<;i++)
{
int nx=r+dx[i];
int ny=c+dy[i];
if(judge(nx,ny))
{
vis[nx][ny]=;
dfs(nx,ny,step+);
vis[nx][ny]=;
}
}
}
int main()
{
int i,t,cas=;
cin>>t;
while(t--)
{
flag=;
cin>>p>>q;
memset(vis,,sizeof(vis));
vis[][]=;
dfs(,,);
printf("Scenario #%d:\n",++cas);
if(flag)
{
for(i=;i<=p*q;i++)
{
printf("%c%d",path[i][]-+'A',path[i][]);
}
}
else
printf("impossible");
printf("\n");
if(t!=)
printf("\n");
}
}

poj-2488 a knight's journey(搜索题)的更多相关文章

  1. Poj 2488 A Knight's Journey(搜索)

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

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

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

  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)

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

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

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

  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 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...

  8. [poj]2488 A Knight's Journey dfs+路径打印

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

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

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

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

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

随机推荐

  1. PHP是.NET上的一门开发语言

    .NET Core 已经实现了PHP JIT,现在PHP是.NET上的一门开发语言   12月23日,由开源中国联合中国电子技术标准化研究院主办的2017源创会年终盛典在北京万豪酒店顺利举行.在本次大 ...

  2. NET Core + Angular 2

    ASP.NET Core + Angular 2 Template for Visual Studio 2017-01-11 08:45 by 小白哥哥, 2069 阅读, 19 评论, 收藏, 编辑 ...

  3. nio aio netty区别

    传统io就是bio     同步阻塞         但可以采用伪同步 nio  jdk1.7以前     同步非阻塞io     1.7以后     同步异步非阻塞                  ...

  4. 不同ORM新的理解

    对于ORM你怎么理解?你用过的ORM有什么区别?这是面试的时候基本上会问的问题. 问题很简单,本文不在阐述.本文主要讨论Dapper 和 EF Core First的区别. 从直观上来看两个都是ORM ...

  5. SQL判断一个事件段 是否在数据库中与其他时间段有重叠 判断时间重叠

    数据库字段startDate 开始时间   endDate 结束时间  -两个参数 比如查2-2  至2-6 在数据库中是否与其他时间有重叠 四个条件有一项满足则有重叠时间 思路是这样子 以开始和结束 ...

  6. SpringBoot 2.0中SpringWebContext 找不到无法使用的问题解决

    为了应对在SpringBoot中的高并发及优化访问速度,我们一般会把页面上的数据查询出来,然后放到redis中进行缓存.减少数据库的压力. 在SpringBoot中一般使用 thymeleafView ...

  7. 【css】css2实现两列三列布局的方法

    前言 对于 flex 弹性布局相信大家都有所了解,它是 css3 中的属性,然而它具有一定的兼容性问题.楼主前几天面试时遇到了面试官需要设计一个两列布局,我当然就说父元素 flex 吧哩吧啦,然而需要 ...

  8. Webpack 入门学习

    1.什么是Webpack? Webpack可以看做是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,TypeScript等 ...

  9. mysql操作封装

    <?php//连接数据库function connect(){  $link = mysql_connect(DB_HOST,DB_USER,DB_PWD)or die("数据库连接失 ...

  10. RK3288开发过程中遇到的问题点和解决方法之Kernel

    修改背光改变区间 kernel\drivers\video\backlight\pwm_bl.c static int pwm_backlight_update_status(struct backl ...