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. DataTable常用操作

    添加列和行的三种方法(转载) 原文地址:http://www.cnblogs.com/jRoger/articles/1887581.html DataTable tblDatas =new Data ...

  2. 服务订单SO创建

    FUNCTION Z_SD_SALESORDER_CREATE. *"------------------------------------------------------------ ...

  3. Objective-C:Foundation框架-常用类-NSValue

    NSNumber是NSValue的子类,前者只能包装数字,后者可以包装任意值.NSArray.NSDictionary只能存储OC对象,不能存储结构体.因此,如果想要在NSArray.NSDictio ...

  4. css hack整理:区别FF,IE8,IE7,IE6,SF,CH浏览器

    css hack整理:区别FF,IE8,IE7,IE6,SF,CH浏览器 2013年03月24日 ⁄ CSS ⁄ 共 716字 ⁄ 暂无评论         前端开发最要命的事就是处理浏览器的兼容性问 ...

  5. 如何在 Linux 上用 SQL 语句来查询 Apache 日志

    Linux 有一个显著的特点,在正常情况下,你可以通过日志分析系统日志来了解你的系统中发生了什么,或正在发生什么.的确,系统日志是系统管理员在解决系统和应用问题时最需要的第一手资源.我们将在这篇文章中 ...

  6. mysql 游标取值为空的问题

    DELIMITER $$ DROP PROCEDURE IF EXISTS updatePic $$ CREATE PROCEDURE updatePic() BEGIN DECLARE cover_ ...

  7. tomcat项目发布 更改小猫图标 及自定义错误404界面

    tomcat发布项目的时候遇到些小问题 不过解决了 问题1. 整个服务器的404自定义界面问题 解决方法: 在tomcat安装目录下conf中web.xml中修改配置文件 <error-page ...

  8. POJ 3249 拓扑排序+DP

    貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...

  9. 我们无法找到服务器加载工作簿的数据模型"的 SharePoint 网站,当您刷新 Excel 2013 工作簿中的数据透视表时出错

    假定您使用 Analysis Services 源在 Microsoft Excel 2013 中创建数据透视表.将 Excel 工作簿上载到 Microsoft SharePoint 网站中.当您尝 ...

  10. 有关C#中使用if else和try catch的问题及效率问题

    本来if esle 是流程控制 try catch是异常处理,本身其实是没有可比性的,但是开发过程中有的人很容易混用,而且两者有的时候效果似乎一样,所以我还是用了个简单的测试来简单的比较下. 不多说, ...