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 ...
随机推荐
- 公司HBase基准性能测试之结果篇
上一篇文章<公司HBase基准性能测试之准备篇>中详细介绍了本次性能测试的基本准备情况,包括测试集群架构.单台机器软硬件配置.测试工具以及测试方法等,在此基础上本篇文章主要介绍HBase在 ...
- OpenCV学习笔记之CXCORE篇
转自blog.csdn.net/bbzz2/article/details/50764209
- 安装ubuntu16.04系统后没有无线网络选项的解决方法
ubuntu系统是自带有无线网络驱动的,因此最好的解决办法是安装是把联网更新选项勾选上,这样在安装是就能自动把无线网络驱动配置好 这是一个比较有效的解决没有无线网络驱动的方法,比后续按网络上的教程自己 ...
- Solr学习笔记之6、Solr学习资源
一.官方资源 1.官网:http://lucene.apache.org/solr/ 2.wiki:http://wiki.apache.org/solr/FrontPage 3.solr中文网:ht ...
- SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]
题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...
- POJ 2987 - Firing - [最大权闭合子图]
题目链接:http://poj.org/problem?id=2987 Time Limit: 5000MS Memory Limit: 131072K Description You’ve fina ...
- 启用yarn日志聚集功能
在yarn-site.xml配置文件中添加如下内容: ##开启日志聚集功能 <property> <name>yarn.log-ag ...
- createrepo local http yum
https://www.jianshu.com/p/59ca879584a1 repodata作为软件的仓库,其目录下有四个必要文件:filelists.xml.[gz],other.xml.[g ...
- 手机e.pageX和e.pageY无效的原因
手机端拖拽事件: touchstart事件:当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发. touchmove事件:当手指在屏幕上滑动的时候连续地触发.在这个事件发生期间,调用prev ...
- 12.预处理数据的方法总结(使用sklearn-preprocessing)
https://blog.csdn.net/sinat_33761963/article/details/53433799