A Knight’s Journey
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 34085 Accepted: 11621

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

知识点:DFS

题意:找到第一条走遍棋盘的路径,并且输出路径。

难点:扩展状态,按‘日’字形扩展。
初始状态:从A1开始,个数为1;
扩展方式:按走‘日’字形8个方向;
目标状态:棋盘全部遍历到

 #include<cstdlib>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char map1[][];
int map2[][];
int vis[][];
int t,p,q,flag;
int dx[] = {-,,-,,-,,-,};
int dy[] = {-,-,-,-,,,,};
void dfs(int x,int y,int cnt)
{
if(cnt==p*q&&!flag)
{
flag=;
for(int i=;i<cnt;i++)
printf("%c%d",map1[i][],map2[i][]);
printf("\n\n");
return;
}
if(x<||x>=p||y<||y>=q)
return;
for(int i=;i<;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
//printf("%d @@@%d %d \n",ny,nx,cnt);
if(!vis[nx][ny]&&nx>=&&nx<p&&ny>=&&ny<q)
{
map1[cnt][]='A'+ny;
map2[cnt][]=nx+;
vis[nx][ny]=;
// printf("%d %d %d \n",ny,nx,cnt);
dfs(nx,ny,cnt+);
vis[nx][ny]=;
}
} }
int main()
{
scanf("%d",&t);
int ha=;
while(t--)
{
scanf("%d%d",&p,&q);
memset(vis,,sizeof(vis));
flag=;
printf("Scenario #%d:\n",++ha);
for(int i=;i<p;i++)
{
for(int j=;j<q;j++)
{
if(flag==)
{
map1[][]='A'+i;
map2[][]=+j+;
vis[i][j]=;
dfs(i,j,);
vis[i][j]=;
}
else
break;
}
if(flag==)
break;
} if(flag==)
printf("impossible\n\n");
} return ;
}
//3
//1 1
//2 3
//4 3

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

A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏的更多相关文章

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

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

  2. HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏

    Sudoku Killer Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会 ...

  3. hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏

    hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...

  4. Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...

  5. Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...

  6. iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  7. IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏

    IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...

  8. C++实现不能被继承的类——终结类 分类: C/C++ 2015-04-06 14:48 64人阅读 评论(0) 收藏

    1.       问题 C++如何实现不能被继承的类,即终结类.Java中有final关键字修饰,C#中有sealed关键字修饰,而C++目前还没有类似的关键字来修饰类实现终结类,需编程人员手动实现. ...

  9. Beautiful People 分类: Brush Mode 2014-10-01 14:33 100人阅读 评论(0) 收藏

    Beautiful People Time Limit: 10000/5000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)   ...

随机推荐

  1. OpenSSL “心脏滴血”漏洞

    OpenSSL "心脏滴血"漏洞 漏洞描述 : OpenSSL软件存在"心脏出血"漏洞,该漏洞使攻击者能够从内存中读取多达64 KB的数据,造成信息泄露. 漏洞 ...

  2. WordPress SEO ☞ WordPress网站终极优化指南

    原文地址:http://www.eastdesign.net/wordpress-seo/ 最新消息,东方设计学院 WordPress SEO 系列视频教程正在持续更新中,目前为了不至于让视频传播过于 ...

  3. 剑指offer-面试题.二叉树的镜像

    题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像.  二叉树节点定义如下: strcut BinaryTreeNode { int val; strcut BinaryTreeNode* m_ ...

  4. MongoDB appendix

    mongo 是数据库shell.一般假定它和mongod 运行在同一台机器上,还假定mongod 绑定了默认端口. eg.  mongo  staging.example.com:20000,这样就会 ...

  5. Class对象

    (一) 获得Class对象的四种方式 第1种方法:Object.getClass() 第2种方法:.class语法 第3种方法:Class.forName() 第4种方法:包装类的TYPE域 impo ...

  6. Struts2(二)——配置文件struts2.xml的编写

    接上一篇博客,这篇博客讲述一下2——9小标题的内容,这些问题都可以在struts2配置文件中设置(当然有的也可以在Struts.properties属性文件,web.xml中进行设置),而且常规开发中 ...

  7. Ubuntu+Eclipse+ADT+Genymotion+VirtualBox开发环境搭建

    1.Eclispe安装就不说了 2.以下说说怎样安装ADT插件.有两种途径: (1)在线安装: 地址:https://dl-ssl.google.com/android/eclipse/(只是近期天朝 ...

  8. pt-online-schema-change解读

    [用途]在线改表 [注意风险]因为涉及到修改表的数据和结构,所以在使用前要小心测试并做好备份,工具默认不会改表,除非你添加了--execute参数 [工具简介] pt-osc模仿MySQL内部的改表方 ...

  9. 【二分查找+优化O(n)】【续UVA1121】Subsequence

    之前的二分答案做法 http://blog.csdn.net/zy691357966/article/details/40212215 二分查找做法: 我们首先试试只枚举终点.对于终点j,我们的目标是 ...

  10. 转:说说JSON和JSONP,也许你会豁然开朗,含jQuery用例

    说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可以通过服 ...