ACM题目————A Knight's Journey
Description
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
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
#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的更多相关文章
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ 2488 A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- TOJ 1702.A Knight's Journey
2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...
- POJ 2488 A Knight's Journey(深搜+回溯)
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- 广大暑假训练1(poj 2488) A Knight's Journey 解题报告
题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A (A - Children of the Candy Corn) ht ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- 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 ...
- HDOJ-三部曲一(搜索、数学)- A Knight's Journey
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- 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 ...
随机推荐
- centos6.5安装logwatch监控日志
Logwatch是使用 Perl 开发的一个日志分析工具Logwatch能够对Linux 的日志文件进行分析,并自动发送mail给相关处理人员,可定制需求Logwatch的mail功能是借助宿主系统自 ...
- MapReduce计算之——hadoop中的Hello World
1. 启动集群 2. 创建input路径(有关hadoop 的命令用 "hadoop fs"),input路径并不能在系统中查找到,可以使用 “hadoop fs -ls /” ...
- 2.3AutoEncoder
AutoEncoder是包含一个压缩和解压缩的过程,属于一种无监督学习的降维技术. 神经网络接受大量信息,有时候接受的数据达到上千万,可以通过压缩 提取原图片最具有代表性的信息,压缩输入的信息量,在将 ...
- 冒泡排序之python
冒泡排序(Bubble sort) 两两比较相邻记录的关键字,如果反序则交换,直到没有反序记录为止. 1.算法描述: 比较相邻的元素.如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工 ...
- Linux umask限制导致php的mkdir 0777无效
原因:mkdir权限受当前linux umask限制 解决方法: $oldmask = umask(0); mkdir("test", 0777); umask($oldmask) ...
- upower xdisplay--nvidia -vga---cpu info
grep 'physical id' /proc/cpuinfo | sort -u | wc -l grep 'core id' /proc/cpuinfo | sort -u | wc -l gr ...
- window10 telnet的启用
(1) window+R打开运行窗口,输入control,如图: (2) 点击类别改成大图标: 如图所示: 然后点击程序和功能. (3) 然后依次点击:启用或者关闭windows功能->teln ...
- windows上使用logstash-input-jdbc
(一)安装logstash 下载链接 选择下载你要的对应的logstash版本,这个东西解压就能使用了 (二)安装logstash-input-jdbc 就是用执行logstash-plugin.b ...
- 【linux & &&命令】&后台(并行)命令 &&串行命令
& 放在一个命令末尾,可以将这个命令放到后台执行.放到后台后主进程将继续向下执行,后台命令将与主进程并行执行. && 放在一个命令末尾,与什么都没有单纯换行实际效果相同,等待 ...
- 洛谷P2322 最短母串问题 [HNOI2006] AC自动机
正解:AC自动机+最短路 解题报告: 传送门! 这题之前考试考到辣,,,我连题目都没看懂这种傻逼事儿就不要说了QAQ 然后就港正解辣 首先这题可以用dp做?等下写 但是一般来说看到这种,第一反应就,先 ...