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
 #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——骑士遍历棋盘,字典序走法的更多相关文章

  1. [itint5]直角路线遍历棋盘

    http://www.itint5.com/oj/#22 这题一开始直接用暴力的DFS来做,果然到25的规模就挂了. vector<bool> visited(50, false); ve ...

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

  3. DFS(二):骑士游历问题

    在国际象棋的棋盘(8行×8列)上放置一个马,按照“马走日字”的规则,马要遍历棋盘,即到达棋盘上的每一格,并且每格只到达一次.例如,下图给出了骑士从坐标(1,5)出发,游历棋盘的一种可能情况. [例1] ...

  4. day53-马踏棋盘

    马踏棋盘 1.算法优化的意义 算法是程序的灵魂,为什么有些程序可以在海量数据计算时,依旧保持高速计算? 编程中算法很多,比如八大排序算法(冒泡.选择.插入.快排.归并.希尔.基数.堆排序).查找算法. ...

  5. [BFS]骑士旅行

    骑士旅行 Description 在一个n m 格子的棋盘上,有一只国际象棋的骑士在棋盘的左下角 (1;1)(如图1),骑士只能根据象棋的规则进行移动,要么横向跳动一格纵向跳动两格,要么纵向跳动一格横 ...

  6. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  7. NOI2.5 1490:A Knight's Journey

    描述 Background The knight is getting bored of seeing the same black and white squares again and again ...

  8. c经典算法

    1. 河内之塔 说明 河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时 北越的首都,即现在的胡志明市:1883年法国数学家 Ed ...

  9. C语言经典算法100例(三)

    1.河内之塔 说明河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市:1883年法国数学家 Edoua ...

随机推荐

  1. 天圆地方&#183; 围棋界的盲棋天才 -- 鲍云

    "鲍云是我心目中继 本因坊秀策,吴清源.武宫正树后第四个我最喜欢的棋手. " 说到盲棋,棋迷们首先想到的绝对是柳大华,外号"东方电脑"的他创造过中国象棋1对19 ...

  2. Swift学习笔记 - 函数与闭包

    import Foundation //1.函数的定义与调用//以 func 作为前缀,返回箭头 -> 表示函数的返回类型func sayHello(name: String) -> St ...

  3. Unity3D Asset stored 已下载的位置

    Unity3D Asset stored下载资源在本地的什么目录里呢?C:\Users\accountName\AppData\Roaming\Unity\Asset Store

  4. hdu 1205

    #include <stdio.h> int a[1005000]; int main() { int t; scanf("%d",&t); while(t-- ...

  5. call和apply区别

    call和apply 基本上是一个意思 区别在于call的第二个参数可以是任意的类型,而apply的第二个参数必须是数组,也可以是arguments.call方法:语法:call(thisObj,Ob ...

  6. Installing node-oracledb on Microsoft Windows

    版本 7 由 Laura Ramsey-Oracle 于 2015-10-19 下午11:46创建,最后由 cj 于 2015-10-22 下午7:44修改. Installing node-orac ...

  7. HTML5触摸屏touch事件使用介绍1

    市面上手机种类繁多,在触屏手机上运行的网页跟传统PC网页相比还是有很大差别的.由于设备的不同浏览器的事件的设计也不同.传统PC站的 click 和 onmouseover 等事件在一般触屏的手机上也可 ...

  8. PHP常用代码大全

    1.连接MYSQL数据库代码 <?php $connec=mysql_connect("localhost","root","root" ...

  9. MySQL MyISAM/InnoDB高并发优化经验

    最近做的一个应用,功能要求非常简单,就是 key/value 形式的存储,简单的 INSERT/SELECT,没有任何复杂查询,唯一的问题是量非常大,如果目前投入使用,初期的单表 insert 频率约 ...

  10. ArcMap - 使用python更新列中的值

    概述:在外文网上,很多人都问在ArcMap中如何通过SQL修改属性字段的值,我见回答的人都说通过"Field Calculator",貌似不能直接通过SQL语句. 虽然学gis开发 ...