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. linux命令行常用快捷键

    方向          <-前               后 ->删除ctrl + d      删除光标所在位置上的字符相当于VIM里x或者dlctrl + h      删除光标所在 ...

  2. 剑指offer-面试题16.反转链表

    题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后的头结点 链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNext; } 其实 ...

  3. Ueditor文本编辑器(新浪SAE平台版本) - 下载频道 - CSDN.NET

    Ueditor文本编辑器(新浪SAE平台版本) - 下载频道 - CSDN.NET Ueditor文本编辑器(新浪SAE平台版本)

  4. hdu 1599 find the mincost route(flyod求最小环)

    Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1, ...

  5. LightOJ 1338 && 1387 - Setu && LightOJ 1433 && CodeForces 246B(水题)

    B - B Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status P ...

  6. 矩阵经典题目六:poj 3070 Fibonacci

    http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...

  7. Xshell同时向多个会话发送指令的方法

    我们平时使用XSHELL.SecureCRT.putty等ssh连接工具连接到远程主机,每次输入指令都是在单一会话窗口,如果有很多台会话,需要同时输入同样的指令,我们就不用一一输入,浪费时间和精力.可 ...

  8. 【转】Java与C#语言级比较

    原文链接:http://www.harding.edu/fmccown/java_csharp_comparison.html Java 程序结构 C# package hello;public cl ...

  9. Docker 生成Node.js web app(含端口映射)

    1.新建目录src,并进入src目录 [xiejdm@localhost Documents]$ mkdir src [xiejdm@localhost Documents]$ cd src/ 2.创 ...

  10. PHP扩展开发之简单类开发

    接下来我们要用扩展的形式实现以下类(演示环境:linux.php-5.5.34-src) <?php class Person { private $_name; public function ...