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. (转)linux下控制帐户过期的多种方法

    linux下控制帐户过期的方法:原文:http://blog.51cto.com/oldboy/1289144企业里一般给无人管理的角色账户或开发人员临时需求等可以设定账户有效期,提升安全!法一:添加 ...

  2. Java微信公众平台开发(十一)--微信JSSDK中Config配置

    JSSDK曾经引爆前端以及后端的工程师,其魔性的力量毋庸置疑,在我们的技术眼里它的实现原理和根本是不能够被改变的,这篇文章就不对其js的实现做任何评价和解说了(因为我也不是很懂,哈哈),这里要说的是它 ...

  3. Zepto事件模块源码分析

    Zepto事件模块源码分析 一.保存事件数据的handlers 我们知道js原生api中要移除事件,需要传入绑定时的回调函数.而Zepto则可以不传入回调函数,直接移除对应类型的所有事件.原因就在于Z ...

  4. mysql非常全的和完整的总结

    (1)数据类型 类型 备注 tinyint/smallint/mediumint/int/bigint 1B/2B/3B/4B/8B float/double 单精度/双精度浮点型 decimal 不 ...

  5. nginx的基本操作

    启动 nginx -c /etc/nginx/nginx.conf停止nginx -s stop nginx -s quit pkill -9 nginx重载nginx -s reload文件检测ng ...

  6. Java输入输出流简单案例

    package com.jckb; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io. ...

  7. VS2012快捷键消失

    我也是网上搜的不过我认为挺有效就自己摘录下来了,具体原作者也找不到,所以就下手了,望原谅. 开始菜单 -->所有程序-->Visual Studio 2012文件夹 --> Visu ...

  8. 【selenium学习笔记一】python + selenium定位页面元素的办法。

    1.什么是Selenium,为什么web测试,大家都用它? Selenium设计初衷就是为web项目的验收测试再开发.内核使用的是javaScript语言编写,几乎支持所以能运行javaScript的 ...

  9. java核心技术 - 17个重要的知识点

    1.Java中没有多继承,而是用接口来代替多继承 2.运行一个已经编译的程序时,Java解释器总是从指定类的main方法中的代码开始执行,因此,执行代码中必须有一个main函数. 3.Java是典型的 ...

  10. 【js类库AngularJs】解决angular+springmvc的post提交问题

    [js类库AngularJs]解决angular+springmvc的post提交问题 AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前 ...