A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

时间:2023-02-17 18:50:21

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数据批量备份还原&lpar;适用于MS SQL 2005&plus;&rpar; 分类: SQL Server 数据库 2015-03-10 14&colon;32 103人阅读 评论&lpar;0&rpar; 收藏

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

  2. 选择排序 分类: 算法 c&sol;c&plus;&plus; 2014-10-10 20&colon;32 509人阅读 评论&lpar;0&rpar; 收藏

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

  3. 1&period;PHP站内搜索 分类: PHP开发实例 2015-07-31 22&colon;48 4人阅读 评论&lpar;0&rpar; 收藏

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

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

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

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

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

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

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

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

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

  8. Hiking 分类: 比赛 HDU 函数 2015-08-09 21&colon;24 3人阅读 评论&lpar;0&rpar; 收藏

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

  9. Sequence 分类: 栈和队列 2015-08-05 10&colon;10 2人阅读 评论&lpar;0&rpar; 收藏

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

随机推荐

  1. Java MD5机密算法的使用

    MD5 是常用的加密算法,是不可逆的.既只能加密,但不能解密. package cn.com.ctsi.csdp.base.util; import java.security.MessageDige ...

  2. Linux内核--网络栈实现分析(十一)--驱动程序层(下)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7555870 更多请查看专栏,地 ...

  3. easyui日期在未加载easyui-lang-zh&lowbar;CN&period;js出现英文的情况下加载中文的方法

    我们有时候在操作easyui的时候本来是加载了easyui-lang-zh_CN.js中文文件包,但是还是出现了英文.使得我们不得埋怨这框架咋这么不好用,其实我们仔细看看这个中文包就会发现里面很多都是 ...

  4. atom 折腾记&lpar;转载的&rpar;

    http://www.bkjia.com/webzh/999078.html

  5. MVC 返回 view

    RedirectToAction(),即直接返回相同Controller的Index方法: 这个方法还有其他重载方法,比如第二个参数是Controller名,可以指定其他Controller下的Vie ...

  6. WPF中利用控件的DataContext属性为多个TextBox绑定数据

    工作上需要从给定的接口获取数据,然后显示在界面的编辑框中,以往肯定会一个一个的去赋值,但这样太麻烦而且效率很低,不利于维护,于是想到了数据绑定这一方法,数据绑定主要利用INotifyPropertyC ...

  7. 如何让浏览器直接输出HTML代码而不解析

    方法一: 将HTML代码嵌入到<script type='text/html' style='display:block'></scipt>中 <script type= ...

  8. sqlserver 收缩数据库&sol;文件

    /******************************/ 1.右键-属性-选项-简单模式 2.右键-任务-收缩-文件 3.右键-任务-收缩-数据库 /********************* ...

  9. linux后台执行&period;&sol;run&period;py提示python syntax error near unexpected token &grave;&lpar;&&num;39&semi;

    python脚本中的#!/usr/bin/python     估计有不少人注意过一些python脚本开头有这么行东东: #!/usr/bin/python 它是用来干嘛的?貌似没有它对脚本功能也没啥 ...

  10. &lbrack;No000016D&rsqb;把知识种进脑子:像读教材一样读书

    读书,常常是书读一遍,过后脑子却空白一片.旁人问起感受,只能以不错.很好作答.更有甚者,有时翻阅豆瓣才发现一本书竟早已「读过」,这事儿可真叫尴尬.为了对付这症状,我笔记也做过,思维导图也画过,奈何只是 ...