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

【题意】给出m*n的方阵(但输入时先输入的是n,再输入m),问马是否能走遍棋盘,输出字典序的第一种路径。

【思路】用mp[i][0]表示第i步所在那个格子的横坐标,用mp[i][1]表示第i步所在那个格子的纵坐标。

字典序的话,注意di数组的顺序。用一个dfs就好啦。

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int vis[N][N];
int mp[N][];
int n,m;
bool flag;
int di[][]={-,-,-,,-,-,-,,,-,,,,-,,};
bool go(int x,int y)
{
if(x<||x>=n||y<||y>=m) return false;
else return true;
} void dfs(int i,int j,int k)
{
if(k==n*m)
{
for(int i=;i<k;i++)
{
printf("%c%d",mp[i][]+'A',mp[i][]+);
}
printf("\n");
flag=true;
// return ;
}
else
for(int x=;x<;x++)
{
int xx=i+di[x][];
int yy=j+di[x][];
if(!vis[xx][yy]&&go(xx,yy)&&!flag)
{
vis[xx][yy]=;
mp[k][]=xx;
mp[k][]=yy;
dfs(xx,yy,k+);
vis[xx][yy]=; }
}
} int main()
{
int t,cas=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
memset(vis,,sizeof(vis));
vis[][]=;
mp[][]=;
mp[][]=;
flag=false;
printf("Scenario #%d:\n",cas++);
dfs(,,); if(!flag) printf("impossible\n");
puts("");
}
return ;
}

A Knight's Journey_DFS的更多相关文章

  1. POJ2488A Knight's Journey[DFS]

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

  2. Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  3. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  4. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  5. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

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

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

  7. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  8. 【POJ 2243】Knight Moves

    题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...

  9. hdu Knight Moves

    这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...

随机推荐

  1. hdu 3172 Virtual Friends (映射并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. addViewController之后view里面的点击事件不响应

    let dealsSeeMoreViewController = DealsSeeMoreViewController(owner: self) self.dealsStackView.addArra ...

  3. NSException

    NSException是什么? 最熟悉的陌生人,这是我对NSException的概述,为什么这么说呢?其实很多开发者接触到NSException的频率非常频繁,但很多人都不知道什么是NSExcepti ...

  4. 20145236 《Java程序设计》第八周学习总结

    20145236 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章 NIO与NIO2 认识NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以让你 ...

  5. HTML5的 2D SVG和SVG DOM的学习笔记(1)

    (项目中要使用SVG,只好补充知识了) HTML体系中,最常用的绘制矢量图的技术是SVG和HTML5新增加的canvas元素.这两种技术都支持绘制矢量图和光栅图. 一.SVG概述 可缩放矢量图形(Sc ...

  6. return false取消手机移动端的默认设置

    想做一个语音界面,当长按语音按钮的时候,总会出现移动端什么复制粘贴菜单.然后在JS中加入return false后就消失了,感觉好神奇哦~

  7. VS构建工具介绍

    VS构建工具介绍 我们都知道C/C++源代码要生成可执行的.exe程序,需要经过编译.链接的过程.你在VS工具中只需要选择菜单Build或按一下F5可以编译.链接.运行了,其实IDE帮我隐藏了好多的具 ...

  8. EF Code First 学习笔记:关系

      一对多关系 项目中最常用到的就是一对多关系了.Code First对一对多关系也有着很好的支持.很多情况下我们都不需要特意的去配置,Code First就能通过一些引用属性.导航属性等检测到模型之 ...

  9. qml android 的一个例子qtHangMan

    这个例子有2个好处: 1.解决了黑屏问题 2.演示了应用内购买的问题

  10. static方法,属性,代码块初始化顺序和执行顺序

    http://greateryang.blog.163.com/blog/static/81953375201232621031508/