POJ_2488——骑士遍历棋盘,字典序走法
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
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
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
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
//lexicographically first path
//题目output中出现了以上几个单词
//就是说骑士的移动步子是按照字典顺序来排列的
//首先对列进行排序较小的在前面,然后按行进行排序也是较小的在前面
//我构造的xy坐标是按照SDL中的图形坐标,x往下递增,y往右递增
//转换成数学上的xy坐标就要先对列x做排序然后再对行y做排序
int dirx[]={-,-,-,-,,,,};
int diry[]={-,,-,,-,,-,}; int T,p,q,mark[][];
std::string way;
bool DFS(int x,int y,int step)
{
if(step==p*q)
{
return true;
}
for(int i=;i<;i++)
{
int dx=x+dirx[i];
int dy=y+diry[i];
if((dx>= && dx<=q) && (dy>= && dy<=p) && mark[dx][dy]!=)
{
mark[dx][dy]=;
//当找到路径才会插入操作,所以在main函数循环中不需要清空string
if(DFS(dx,dy,step+))
{
//在第0个位置插入1个字符
//先插入数字然后再插入字母,否则顺序颠倒
way.insert(,,dy+'');
way.insert(,,dx+'A'-);
return true;
}
mark[dx][dy]=;
}
}
return false;
}
int main()
{
scanf("%d",&T);
for(int i=;i<=T;i++)
{
scanf("%d%d",&p,&q);
way.clear();//每次都要清空string
bool find=false;
for(int x=;x<=q && !find;x++)
{
for(int y=;y<=p;y++)
{
memset(mark,,sizeof(mark));
mark[x][y]=;
if(DFS(x,y,))
{
//找到的话插入起始位置
way.insert(,,y+'');
way.insert(,,x+'A'-);
find=true;
break;
}
}
}
std::cout<<"Scenario #"<<i<<":"<<std::endl;
if(find)
{
//整体输出就行
std::cout<<way<<std::endl<<std::endl;
}
else
printf("impossible\n\n");
}
return ;
}
POJ_2488——骑士遍历棋盘,字典序走法的更多相关文章
- [itint5]直角路线遍历棋盘
http://www.itint5.com/oj/#22 这题一开始直接用暴力的DFS来做,果然到25的规模就挂了. vector<bool> visited(50, false); ve ...
- [LeetCode] Knight Probability in Chessboard 棋盘上骑士的可能性
On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K ...
- DFS(二):骑士游历问题
在国际象棋的棋盘(8行×8列)上放置一个马,按照“马走日字”的规则,马要遍历棋盘,即到达棋盘上的每一格,并且每格只到达一次.例如,下图给出了骑士从坐标(1,5)出发,游历棋盘的一种可能情况. [例1] ...
- day53-马踏棋盘
马踏棋盘 1.算法优化的意义 算法是程序的灵魂,为什么有些程序可以在海量数据计算时,依旧保持高速计算? 编程中算法很多,比如八大排序算法(冒泡.选择.插入.快排.归并.希尔.基数.堆排序).查找算法. ...
- [BFS]骑士旅行
骑士旅行 Description 在一个n m 格子的棋盘上,有一只国际象棋的骑士在棋盘的左下角 (1;1)(如图1),骑士只能根据象棋的规则进行移动,要么横向跳动一格纵向跳动两格,要么纵向跳动一格横 ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- NOI2.5 1490:A Knight's Journey
描述 Background The knight is getting bored of seeing the same black and white squares again and again ...
- c经典算法
1. 河内之塔 说明 河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时 北越的首都,即现在的胡志明市:1883年法国数学家 Ed ...
- C语言经典算法100例(三)
1.河内之塔 说明河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市:1883年法国数学家 Edoua ...
随机推荐
- POJ Farm Tour
Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家 ...
- valgrind 的使用及错误信息分析
这里记录一下使用valgrind查找你的应用程序中的各种潜在的错误信息,并举例说明. 经常使用valgrind查找一下你的代码的内存有关错误,对移植到嵌入系统后的系统稳定性来说有着重要的意 ...
- C#时间戳的获取与转换方法
/// <summary> /// 获取当前时间戳 /// </summary> /// <param name="bflag"></pa ...
- 27个Jupyter快捷键、技巧(原英文版)
本文是转发自:https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/ 的一篇文章,先记录在此,等有空时我会翻译成中文 ...
- 面试题 HashMap 数据结构 实现原理
数据结构 HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组:数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O ...
- Java多线程——线程同步
在之前,已经学习到了线程的创建和状态控制,但是每个线程之间几乎都没有什么太大的联系.可是有的时候,可能存在多个线程多同一个数据进行操作,这样,可能就会引用各种奇怪的问题.现在就来学习多线程对数据访问的 ...
- codevs 2800 送外卖(状压dp)
/* f[i][j] 表示走过的点构成i状态 且最后到达的点为j时的最优解 在那最后一个状态就是(1<<n+1)-1 每个点都到达 在由此回到0 */ #include<iostre ...
- android系统360浏览器使用append动态添加元素出现黑色错乱背景
去掉样式 backface-visibility:hidden;
- Examples_07_06 无法下载android的sdk
在hosts里面配置. 74.125.237.1 dl-ssl.google.com 在AndroidManifest.xml中添加 <uses-feature android:name=&qu ...
- java socket报文通信(三)java对象和xml格式文件的相互转换
前两节讲了socket服务端,客户端的建立以及报文的封装.今天就来讲一下java对象和xml格式文件的相互转换. 上一节中我们列举了一个报文格式,其实我们可以理解为其实就是一个字符串.但是我们不可能每 ...