A Knight’s Journey

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 35564 Accepted: 12119

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

Source

TUD Programming Contest 2005, Darmstadt, Germany

一段时间没有做搜索,果然手生了很多,犯了各种各样的错误,要赶紧补回来.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#pragma comment(linker,"/STACK:102400000")
#define WW freopen("output.txt","w",stdout) struct node
{
int x;
int y;
} a[100]; int Dir[][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; bool vis[9][9]; int n,m; int flag; bool DFS(int x,int y,int site)
{
vis[x][y]=true;
a[site].x=x;
a[site].y=y;
if(site==n*m)
{
flag=true;
return true;
}
int Fx,Fy;
for(int i=0; i<8; i++)
{
Fx=x+Dir[i][0];
Fy=y+Dir[i][1];
if(Fx>=0&&Fx<n&&Fy>=0&&Fy<m&&!vis[Fx][Fy])
{
if(DFS(Fx,Fy,site+1))
{
return true;
} }
}
vis[x][y]=false;
return false;
} int main()
{ int T,w=1;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
flag=false;
memset(vis,false,sizeof(vis));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(DFS(i,j,1))
{
flag=true;
break;
}
}
if(flag)
{
break;
}
}
printf("Scenario #%d:\n",w++);
if(flag)
{
for(int i=1; i<=n*m; i++)
{
printf("%c%c",a[i].y+'A',a[i].x+1+'0');
}
printf("\n\n");
}
else
{
printf("impossible\n\n");
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏的更多相关文章

  1. MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏

    我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...

  2. 选择排序 分类: 算法 c/c++ 2014-10-10 20:32 509人阅读 评论(0) 收藏

    选择排序(假设递增排序) 每次选取从当前结点到末尾结点中最小的一个与当前结点交换,每一轮固定一个元素位置. 时间复杂度O(n^2),空间复杂度O(1).下面的示例代码以带头结点的链表为存储结构: #i ...

  3. 1.PHP站内搜索 分类: PHP开发实例 2015-07-31 22:48 4人阅读 评论(0) 收藏

    PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...

  4. Ombrophobic Bovines 分类: POJ 图论 最短路 查找 2015-08-10 20:32 2人阅读 评论(0) 收藏

    Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16539 Accepted: 3605 ...

  5. Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏

    Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...

  6. MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏

    ************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释 ...

  7. 利用OpenMP实现埃拉托斯特尼(Eratosthenes)素数筛法并行化 分类: 算法与数据结构 2015-05-09 12:24 157人阅读 评论(0) 收藏

    1.算法简介 1.1筛法起源 筛法是一种简单检定素数的算法.据说是古希腊的埃拉托斯特尼(Eratosthenes,约公元前274-194年)发明的,又称埃拉托斯特尼筛法(sieve of Eratos ...

  8. Hiking 分类: 比赛 HDU 函数 2015-08-09 21:24 3人阅读 评论(0) 收藏

    Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  9. Sequence 分类: 栈和队列 2015-08-05 10:10 2人阅读 评论(0) 收藏

    Sequence Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8277 Accepted: 2708 Description ...

随机推荐

  1. PostgreSQL Replication之第十二章 与Postgres-XC一起工作(1)

    在本章中,我们希望将我们的注意力集中在写可扩展,多主,同步,对称和PostgreSQL的称为Postgres-XC(PostgreSQL eXtensible Cluster)的透明复制方案.该项目的 ...

  2. 最长上升子序列(N*log(N))hdu1025

    (HDU1025) Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. spring day01-go1

    1.复制xml到container/ioc下2.B类实现序列化接口,构造函数和f1函数A类实现序列化接口,将B类作为其成员变量,且get/set方法,执行execute方法去调用b.f1()3.修改x ...

  4. paper 20 :color moments

    图像的颜色特征是很重要的,其中颜色矩也是很重要的一部分.(还有一个关于图像颜色特征的review,对于image color写的比较全面).还有,我要强调一下,本blog上的链接都是Google学术上 ...

  5. 夺命雷公狗---DEDECMS----18dedecms之无可奈何标签-sql标签取出今天更新

    我们在一些开发时候遇到普通标签都解决不了的问题的时候可以尝试下我们dedecms自带的sql标签,几乎可以完成任何的查询需求 语法如下所示: 我们在这里将刚才首页今天更新那块给改写下,原先的是: {d ...

  6. MOPSO 多目标例子群优化算法

    近年来,基于启发式的多目标优化技术得到了很大的发展,研究表明该技术比经典方法更实用和高效.有代表性的多目标优化算法主要有NSGA.NSGA-II.SPEA.SPEA2.PAES和PESA等.粒子群优化 ...

  7. viewpager+fragment+HorizontalScrollView

    xml布局 <RelativeLayout        android:id="@+id/rl_column"        android:layout_width=&q ...

  8. ORA-000845 与 /dev/shm(tempfs)

    MEMORY_TARGET参数在Oracle 11g被引进,主要是用于控制Oracle对于系统内存的使用,首次将SGA与PGA整合到一起实现自动管理.一旦设置了MEMORY_TARGET参数值,Ora ...

  9. COM编程之四 引用计数

    [1]客户为什么不应直接控制组件的生命期? 假设一个组件A正在使用另一个组件B,可想组件A(客户)代码中肯定有若干个指向组件B接口的指针. 那么这种情况下,当使用完一个接口而仍然在使用另一个接口时,是 ...

  10. 【python cookbook】【字符串与文本】4.文本模式的匹配和查找

    问题:按照特定的文本模式进行匹配或查找 解决方法: 1.简单的文字匹配,只需使用str.find().str.startswith().str.endswith()或类似的函数即可: 2.复杂的匹配, ...