A Knight's Journey

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24840   Accepted: 8412

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

简单的深搜,不多说,直接上代码!

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int pathlow[30],pathdown[30],visit[30][30];
int dir[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}},n,m;//这里注意是字典序最小
bool dfs(int low,int down,int num)
{
int i,x,y;
if(num==n*m)
{ for(i=0;i<n*m;i++)
{ printf("%c%d",'A'+pathdown[i],pathlow[i]+1);
}
return true;
} for(i=0;i<8;i++)
{
x=low+dir[i][0];
y=down+dir[i][1];
pathlow[num]=x;
pathdown[num]=y;
if(x>=0&&x<n&&y>=0&&y<m&&(!visit[x][y]))
{ visit[x][y]=1;
if(dfs(x,y,num+1))
{ return true;
}
else
{ visit[x][y]=0;//这里要注意,一定要重新标记为0
}
} }
return false; }
int main ()
{
int t,i;
while(scanf("%d",&t)!=EOF)
{ for(i=1;i<=t;i++)
{
printf("Scenario #%d:\n",i);
scanf("%d%d",&n,&m);
memset(visit,0,sizeof(visit));
visit[0][0]=1;
pathlow[0]=0;
pathdown[0]=0;
if(!dfs(0,0,1))
{ printf("impossible");
}
printf("\n\n"); }
} return 0;
}

poj2488 A Knight's Journey的更多相关文章

  1. POJ2488A Knight's Journey[DFS]

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

  2. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  3. POJ2488A Knight's Journey

    http://poj.org/problem?id=2488 题意 : 给你棋盘大小,判断马能否走完棋盘上所有格子,前提是不走已经走过的格子,然后输出时按照字典序排序的第一种路径 思路 : 这个题吧, ...

  4. poj2488--A Knight&#39;s Journey(dfs,骑士问题)

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

  5. 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 ...

  6. HDOJ-三部曲一(搜索、数学)- A Knight's Journey

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  7. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...

  8. A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏

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

  9. TOJ 1702.A Knight's Journey

    2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...

随机推荐

  1. Pattern | CLiPS

    Pattern | CLiPS Pattern Pattern is a web mining module for the Python programming language. It has t ...

  2. Js 中常用方法

    一.获取唯一值(2014-12-23) function newGuid() { var guid = ""; var n = (((1 + Math.random()) * 0x ...

  3. Trie树|字典树(字符串排序)

    有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...

  4. 《Qt编程的艺术》——5.1 手动布局

    在传统的GUI设计中,每个控件(Widget)都要手动地绑定在窗口之上的一个点上(也就是说,这个控件被指定成了给定GUI元素的父对象),同时还要指定这个控件的高度和宽度.作为所有图形元素的基础类,QW ...

  5. stagefright框架(三)-选择Video Decode

    在<Stagefright (1) – Video Playback的流程>中,我们并没有详述Stagefright是如何根据影片档的类型来选择适合的video decoder,现在,就让 ...

  6. 网页被Chrome识别成英语,区域,语言,网站

    修改成这个后解决 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  7. .NET进阶系列之一:C#正则表达式整理备忘

    有一段时间,正则表达式学习很火热很潮流,当时在CSDN一天就能看到 好几个正则表达式的帖子,那段时间借助论坛以及Wrox Press出版的<C#字符串和正则表达式参考手册>学习了一些基础的 ...

  8. Spring学习之Ioc控制反转(1)

    开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...

  9. V - 不容易系列之(4)――考新郎(第二季水)

    Description          国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这 ...

  10. 在Struts2中使用poi进行excel操作下载的时候报getOutputStream() has already been called for this response 错误 [转]

    在项目中用到了poi这个开源的操作excel文件的jar. 项目中用到struts2容器管理servlet.不是单纯的直接用servlet.         workbook.write(os);   ...