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就好,只是要记住路径:
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
int dx[]={-1, 1, -2, 2, -2, 2, -1, 1}, dy[] = {-2, -2, -1, -1, 1, 1, 2, 2};
int path[30][30], vis[30][30], p, q, cnt;
bool flag; void DFS(int r, int c, int sp)
{
path[sp][0] = r ;
path[sp][1] = c ;
if(sp == p*q )
{
flag = 1 ;
return ;
}
for(int i=0; i<8; i++)
{
int x = r + dx[i] ;
int y = c + dy[i] ;
if(x>=1 && x<=p && y>=1 && y<=q && !vis[x][y] && !flag)
{
vis[x][y] = 1 ;
DFS(x,y,sp+1);
vis[x][y] = 0 ;
}
}
} int main()
{
int n, k;
cin >> n ;
for(k=1; k<=n; k++)
{
flag = 0 ;
cin >> p >> q ;
memset(vis,0,sizeof(vis));
vis[1][1] = 1;
DFS(1,1,1);
cout << "Scenario #" << k << ":" << endl ;
if(flag)
{
for(int i=1; i<=p*q; i++)
printf("%c%d",path[i][1]-1+'A',path[i][0]);
}
else cout << "impossible" ;
cout << endl ;
if(k!=n) cout << endl ;
} return 0;
}

ACM题目————A Knight's Journey的更多相关文章

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

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

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

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

  3. TOJ 1702.A Knight's Journey

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

  4. POJ 2488 A Knight's Journey(深搜+回溯)

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

  5. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  6. POJ2488A Knight's Journey[DFS]

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

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

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

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

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

随机推荐

  1. OpenCV Cut Image via ROI 根据兴趣区域剪裁图片

    我们在使用OpenCV时,有时候需要对现有图片剪裁,比如只需要一个小窗口中的内容,那么我们可以通过OpenCV的兴趣区域 Region of Interest (ROI) 来很轻易的实现,操作也很简单 ...

  2. pandas绘图总结

    转自:http://blog.csdn.net/genome_denovo/article/details/78322628 pandas绘图总结 pandas中的绘图函数(更加详细的绘图资料可参考p ...

  3. [python-opencv]模板匹配

    模板匹配最适用于工业场合(在一张图片中识别特定的工件图) 模板匹配是一种最原始.最基本的模式识别方法,研究某一特定对象物的图案位于图像(target)的什么地方,进而识别对象物,这就是一个匹配问题. ...

  4. IO流(6)获取功能

    获取功能: * public String getAbsolutePath():获取绝对路径 * public String getPath():获取相对路径 * public String getN ...

  5. EL--Expression Language

    EL 存取变量数据的方法很简单,例如:${username}.它的意思是取出某一范围中名称为username的变量.因为我们并没有指定哪一个 范围的username,所以它的默认值会先从Page 范围 ...

  6. 带分数dfs+剪枝优化

    #include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>using name ...

  7. Tensorflow(二)

    1---------------- 试用tensorflow的模块,必须配套tensorflow的方法 import tensorflow as tf a=3 ##定义 行向量 w=tf.Variab ...

  8. [git]git版本管理学习记录

    今天看到别人用这玩意记录自己的进度, 我也学习了一下. 1,适当的工具会提升效率 2,关注点还是得放在代码本身上. github/gitignore github提供了各种gitignore文件 有p ...

  9. 性能测试之nmon对linux服务器的监控

    大家都知道在做性能测试的时候,需要监控服务器的资源情况,而大多数服务器是Linux系统,网上资料嘿多,这里汇总介绍下Nmon监控工具: -------------------------------- ...

  10. mysql 用户授权命令

    mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_root_password' WITH GRANT OP ...