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. linux----关于定位和查找

    1.top --查看进程2.su --临时切换用户命令[root@tomato2 ~]# sudo su gongxijun[gongxijun@tomato2 root]$ 3.whoami --- ...

  2. (转)codeblock(常用快键)

    一款开源的C/C++ IDE(集成开发环境),基于wxWidgets GUI体系,跨平台支持. 编辑器 快捷键 功能 Ctrl+Z 恢复上一次操作 Ctrl+Shift+Z 重复上一次操作 F11 切 ...

  3. App开发

    设置App图标 在Assets.xcassets的AppIcon中添加图片. 设置App名称 工程 -> Info -> 添加Key:“Bundle Display Name“ 和 Val ...

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

    20145236 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 一.继承 •继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类.继承可以理解 ...

  5. js事件知识整理

    鼠标事件 鼠标移动到目标元素上的那一刻,首先触发mouseover 之后如果光标继续在元素上移动,则不断触发mousemove 如果按下鼠标上的设备(左键,右键,滚轮……),则触发mousedown ...

  6. Mysql 修改列的顺序

    alter table 表名 modify 字段名 字段类型 after 字段举例alter table user_info modify user_name varchar(10) after us ...

  7. c++实现之 -- 汉语词语的简单处理

    好了,我们现在已经会怎样读入了,然后就是研究一下如何存储等一些细节上的的问题了. 首先,比较函数是不能传入char*的地址的,但是可以接受一个string类. 然而,如果是两个比较长的string类, ...

  8. BroadcastReceiver的实例----基于Service的音乐播放器之一

    下面的程序开发了一个基于Service的音乐盒,程序的音乐将会由后台运行的Service组件负责播放,当后台的播放状态发生改变时,程序将会通过发送广播通知前台Activity更新界面:当用户单击前台A ...

  9. C# 多线程详解 Part.03 (定时器)

    Timer 类:     设置一个定时器,定时执行用户指定的函数.定时器启动后,系统将自动建立一个新的线程,执行用户指定的函数. using System; using System.Threadin ...

  10. bzoj 2242: [SDOI2011]计算器

    #include<cstdio> #include<iostream> #include<map> #include<cmath> #define ll ...