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. C# 父子类_实例_静态成员变量_构造函数的执行顺序

    今天去面试的时候被一道题问得一点脾气都没有,今天特地来研究下. 子类成员变量,子类静态成员变量,子类构造函数,父类成员变量,父类静态成员变量,父类构造函数的执行顺序. 现在贴上从另外一个.net程序员 ...

  2. xampp安装时mysql报错

    问题描述:以前安装过mysql,后来安装xampp,mysql打不开,出错提示16:04:48  [mysql]  MySQL Service detected with wrong path16:0 ...

  3. Common Lisp 编译器IDE环境搭建

    搭建Common Lisp编程环境的方法有很多种,这里我使用的是最常见的一种:SBCL + Emacs + SLIME. SBCL是Steel Bank Common Lisp的简称,它是Common ...

  4. 【C语言用法】C语言的函数“重载”

    由于平时很少用到__attribute__定义函数或者变量的符号属性,所以很难想象C语言可以向C++一样进行函数或者变量的重载. 首先,复习一下有关强符号与弱符号的概念和编译器对强弱符号的处理规则: ...

  5. tomcat7 https 成功测试

    1,生成证书

  6. 云脉提供表单识别API接口自助接入

    如今随着信息化.数字化时代的到来,利用纯人工进行数据录入已经不能满足海量信息数字化的需求.这时候有OCR表单识别技术,一切问题都能够迎刃而解了.云脉表单识别SDK采用成熟的OCR技术,通过创建票据的模 ...

  7. 走进C++程序世界------继承和派生

    继承和派生 继承是面向对象编程语言的最重要方面之一,正确的使用继承可编写出设计良好,容易于维护和扩展的应用程序.下面是在其他博客中的总结: ****************************** ...

  8. ios像素点颜色取样

    一.像素点颜色取样 + (UIColor*) getPixelColorAtLocation:(CGPoint)point inImage:(UIImage *)image { UIColor* co ...

  9. struts2的<constant/>标签使用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...

  10. 【错排问题】【HDU2048】神、上帝以及老天爷

    神.上帝以及老天爷 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...