A Knight's Journey
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29226   Accepted: 10023

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

深索水题。,,。主要是方向被规定了。

AC代码例如以下:

#include<iostream>
#include<cstring>
using namespace std; struct H
{
int x;
char y;
}b[30],c[30]; int dx[8]={-1,1,-2,2,-2,2,-1,1};
int dy[8]={-2,-2,-1,-1,1,1,2,2}; int a[30][30],vis[30][30];
int n,m,bj; void dfs(int h,int z,int cur)
{
int i;
if(cur==n*m)
{
if(bj==0)
{
for(i=0;i<cur;i++)
cout<<b[i].y<<b[i].x;
cout<<endl<<endl;
bj=1;
} }
else
{
for(i=0;i<8;i++)
{
int xx,yy;
xx=h+dx[i];
yy=z+dy[i];
if(!vis[xx][yy]&&xx>=1&&xx<=n&&yy>=1&&yy<=m)
{
vis[xx][yy]=1;
b[cur].x=xx;b[cur].y=(char)(yy+'A'-1);
dfs(xx,yy,cur+1);
vis[xx][yy]=0;
}
} }
} int main()
{
int t,cas=0;
cin>>t;
while(t--)
{
cas++;
memset(a,0,sizeof a);
memset(vis,0,sizeof vis);
cin>>n>>m;
bj=0;
b[0].x=1;b[0].y=1+'A'-1;
vis[1][1]=1;
cout<<"Scenario #"<<cas<<":"<<endl;
dfs(1,1,1);
if(bj==0)
{
cout<<"impossible"<<endl<<endl;
}
}
return 0;
}

POJ 2488 A Knight&#39;s Journey的更多相关文章

  1. poj 2488 A Knight&#39;s Journey(dfs+字典序路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...

  2. pku 2488 A Knight&#39;s Journey (搜索 DFS)

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28697   Accepted: 98 ...

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

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

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

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

  5. POJ 2488-A Knight&#39;s Journey(DFS)

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10 ...

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

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

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

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

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

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

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

随机推荐

  1. CentOS7.4 关闭firewall防火墙,改用iptables

    1.关闭默认的firewall防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service ...

  2. 【贪心】Google Code Jam Round 1A 2018 Waffle Choppers

    题意:给你一个矩阵,有些点是黑的,让你横切h刀,纵切v刀,问你是否能让切出的所有子矩阵的黑点数量相等. 设黑点总数为sum,sum必须能整除(h+1),进而sum/(h+1)必须能整除(v+1). 先 ...

  3. 【原创】实战padding oracle漏洞

    首先关于padding oracle漏洞的原理请看: 步入正传~~ 搭建漏洞利用环境Perl 环境下载地址:链接:http://pan.baidu.com/s/1skFxVm1 密码:anuw 首先查 ...

  4. hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...

  5. opencv第三课 Canny边缘检测

    #include<stdio.h> #include<iostream> #include<opencv2\opencv.hpp> using namespace ...

  6. HDU 5137 How Many Maos Does the Guanxi Worth 最短路 dijkstra

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  7. 《学习OpenCv》 笔记(1)

    P1-P17 废话 可跳过 不过讲了如何搭建环境,如果你没有搭建的话,可以查看我的另外一个博文,详细讲了如何构建OpenCv的编程环境 P19 开始编写第一个代码

  8. flex socket policy

    @ flex的as3代码是具备使用origin tcp socket通信能力的. @ 如果是flex builder本机调试,那么可以直连tcp的server. @ 如果flex发布在webserve ...

  9. rac 10g 10.2.0.1升级到10.2.0.5具体解释

        RAC 10.2.0.1 升级到 10.2.0.5 一. 准备: Patch 包:p8202632_10205_LINUX.zip   节点数:3个节点       RAC1    RAC2  ...

  10. SpringMVC和Springboot的区别(网摘)

    spring boot 我理解就是把 spring spring mvc spring data jpa 等等的一些常用的常用的基础框架组合起来,提供默认的配置,然后提供可插拔的设计,就是各种 sta ...