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. FileAttributes枚举

    FileAttributes枚举是一个专门用于标记硬盘上的文件属性的枚举,枚举的说明在这里:http://www.cnblogs.com/kissdodog/archive/2013/01/16/28 ...

  2. Linux和windows动态库

    转载:http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html 态链接库技术实现和设计程序常用的技术,在Windows和Linux系 统中 ...

  3. Difference Between Mod_Python & Mod_Wsgi | eHow

    Difference Between Mod_Python & Mod_Wsgi | eHow     x    YES    NO    Why not?     Thanks for he ...

  4. 使用GridBagLayout控制行列的高度和宽度

    摘自http://bbs.csdn.net/topics/340189065使用GridBagLayout控制行列的高度和宽度 gridwidth 指定组件显示区域的某一行中的单元格数. 默认值1,水 ...

  5. 【转】android 电池(三):android电池系统

    关键词:android电池系统电池系统架构 uevent power_supply驱动 平台信息: 内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV3 ...

  6. poj 2446 (二分匹配)

    题意:除了所给的一些点外,问能不能用1*2的矩形覆盖所有的点,矩形间不能重叠. 思路:简单二分匹配,,,,,,, #include<stdio.h> #include<string. ...

  7. ORM框架Hibernate (一) 对DAO封装和抽象

    说明 前面已经给大家介绍了Struts这个框架,Struts是对Web项目的表示层进行了封装,而Hibernate是对Web项目中DAO层进行封装,也即是.NET中我们常用到的D层封装,即对访问数据库 ...

  8. linux下挂载iso镜像文件(转)

    挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式: mount [-t vfstype] [-o optio ...

  9. PropertyGrid—添加属性Tab

    零.引言 PropertyGrid用来显示和编辑对象的属性,前面已经简单介绍了如何使用该控件和提供不同的属性编辑方法.前面主要讲如何使用该控件,但有时,该控件无法满足我们的需求,就需要对其进行扩展.本 ...

  10. zepto的touch.js左右滑动存在一些问题,用百度的touch.js代替

    这几天用zepto想写一个移动端的活动,在实现左右滑动触发动画时,发现zepto的touch.js在ios的微信上有问题. 问题描述:左右滑动时如果手指没有一直跟频幕贴着(在手机上滑动时,如果手指不是 ...