POJ 2488 A Knight's Journey(DFS)
A Knight's Journey
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 34633
Accepted: 11815
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
题目简单翻译:
给你一个象棋中的马,一个n*m的棋盘,求是否能从一点出发,走遍整个棋盘,不重复走。如果能,按字典序输出第一个序列。如果不能,则输出“impossible”。
解题思路:
dfs,从一点出发,然而因为要字典序较小的,我们就选择(1,1)为起始点吧。注意延伸的方向,优先向字典序小的方向延伸。
代码:
#include<cstdio>
#include<cstring>
#include<queue> using namespace std;
int n,m;
int vis[][];
int dx[]={-,-,-,-,,,,};
int dy[]={-,,-,,-,,-,};
int a1[],a2[];
bool check(int x,int y)
{
return x>=&&x<n&&y>=&&y<m;
}
bool dfs(int x,int y,int depth)
{
if(depth==m*n)
{
for(int i=;i<depth;i++)
printf("%c%d",a1[i]+'A',a2[i]+);
puts("");
return true;
}
for(int i=;i<;i++)
{
int curx=x+dx[i];
int cury=y+dy[i];
if(check(curx,cury)&&vis[curx][cury]==)
{
a1[depth]=curx;
a2[depth]=cury;
vis[curx][cury]=;
if(dfs(curx,cury,depth+)) return true;
vis[curx][cury]=;
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
int flag=;
while(T--)
{
if(flag) puts("");
scanf("%d%d",&m,&n);
memset(vis,,sizeof vis);
vis[][]=;
a1[]=,a2[]=;
printf("Scenario #%d:\n",++flag);
if(!dfs(,,)) puts("impossible");
}
return ;
}
POJ 2488 A Knight's Journey(DFS)的更多相关文章
- POJ 2488 A Knight's Journey (DFS)
poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...
- poj 2488 A Knight's Journey( dfs )
题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- POJ 2488 A Knight's Journey(深搜+回溯)
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- POJ 2488 A Knight's Journey【DFS】
补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...
- A Knight's Journey (DFS)
题目: Background The knight is getting bored of seeing the same black and white squares again and agai ...
- POJ 2488 -- A Knight's Journey(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ 2488-A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31702 Accepted: 10 ...
- poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】
题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...
随机推荐
- angular--bootstrap实例日期控件【datepicker】
head部分: <!--Bootstrap--> <link rel="stylesheet" href="/supProdom/script/boot ...
- RAC之RMAN恢复
之前整理的RMAN 有关还原的文章: RMAN 系列(五) ---- RMAN 还原 与 恢复 http://blog.csdn.net/tianlesoftware/archive/2010/07/ ...
- Linux系统监控
http://my.oschina.net/aiguozhe/blog/35730 http://my.oschina.net/aiguozhe/blog/35730
- Linux备份与恢复
确定要备份的内容 在备份和还原系统时,Linux 基于文件的性质成了一个极大的优点.在 Windows 系统中,注册表与系统是非常相关的.配置和软件安装不仅仅是将文件放到系统上.因此,还原系统就需要有 ...
- Qt编程之实现在QFileDialog上添加自定义的widget
上网搜索找到的方法如下: http://www.qtforum.org/article/20841/how-to-add-a-qwidget-in-qfiledialog.html#post78422 ...
- android UI之Shape详解_GradientDrawable
在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片. 这样就容易使apk变大. 那 ...
- 程序员求职之道(《程序员面试笔试宝典》)之求职有用网站及QQ群一览表
技术学习网站 www.csdn.com www.iteye.com www.51cto.com http://www.cnblogs.com/ http://oj.leetcode.com/ http ...
- 自己写jstl标签解析long时间
数据库里存储的是long型的时间,现在想输出到jsp页面,由于使用的是jstl标签,而要显示的是可读的时间类型,找来找去有个fmt:formatDate可以转化,但是只能转date型,long型则不可 ...
- poj 2096 Collecting Bugs(期望 dp 概率 推导 分类讨论)
Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other ...
- TypeScript 素描 - 模块解析、声明合并
模块解析 模块解析有两种方式 相对方式 也就是以/或 ./或-/开头的,比如import jq from "/jq" 非相对方式 比如 import model from ...