A Knight's Journey(dfs)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 25950 | Accepted: 8853 |
Description
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
Output
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 题意:给出p和q,p代表行数(1,2,3....),q代表列数(A,B,C....),要求输出骑士从任意一点出发经过所有点的路径,必须按字典序输出;路径不存在输出impossible; 思路:与dfs模板不同的是路径按字典序输出,所以dfs的顺序就不是随意的了,必须按dir[8][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}的顺序;
而且起点必须是A1,这样得出的路径字典序才最小;
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; struct node
{
int row;
int col;
}way[];//记录所走路径的行和列 int p,q;
bool vis['Z'+][];
int dir[][] = {{-,-},{,-},{-,-},{,-},{-,},{,},{-,},{,}}; bool DFS(struct node* way,int i,int j,int step)
{
vis[i][j]=true;
way[step].row=i;
way[step].col=j;
if(step==way[].row)
return true; for(int k=; k<; k++)//向八个方向走
{
int ii = i+dir[k][];
int jj = j+dir[k][];
if(!vis[ii][jj] && ii>= && ii<=p && jj>= && jj<=q)
if(DFS(way,ii,jj,step+))
return true;
} vis[i][j]=false;
return false;
} int main()
{
int test;
scanf("%d",&test);
for(int t = ; t <= test; t++)
{
memset(vis,false,sizeof(vis));
scanf("%d %d",&p,&q); way[].row =p*q; if(DFS(way,,,))
{
cout<<"Scenario #"<<t<<':'<<endl; for(int k=; k<=way[].row; k++)
cout<<(char)(way[k].col-+'A')<<way[k].row;
cout<<endl<<endl; } else
{
cout<<"Scenario #"<<t<<':'<<endl;
cout<<"impossible"<<endl<<endl;
}
}
return ;
}
A Knight's Journey(dfs)的更多相关文章
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- 迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs) POJ - 3984 #include <iostream> #include <queue> #include <stack> #incl ...
- POJ2488:A Knight's Journey(dfs)
http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- POJ2248 A Knight's Journey(DFS)
题目链接. 题目大意: 给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点. 列为数字,行为字母,搜索按字典序. 分析: 用 vis[x][y] 标记是否已经访问.因为要搜索所 ...
- 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: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏
A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...
- poj2488 A Knight's Journey裸dfs
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35868 Accepted: 12 ...
随机推荐
- python s12 day2
python s12 day2 入门知识拾遗 http://www.cnblogs.com/wupeiqi/articles/4906230.html 基本数据类型 注:查看对象相关成员 var, ...
- NetworkOnMainThreadException
来自:http://www.2cto.com/kf/201402/281526.html NetworkOnMainThreadException extends RuntimeException j ...
- 【css面试题】三个DIV要求水平对齐,左右两个DIV宽度固定为100px,中间那个DIV充满剩余的宽度(至少2种方法)
这是我在一家公司面试时遇到的问题,当时没有答上来!! 所以看到的小伙伴一定要注意了!! 变化浏览器宽度可看到效果: 左 右 中 然后我们来看看代码: 第一种方法:(浮动) <style type ...
- inner join
select Person.LastName,Person.FirstName,Orders.OrderNo from Persons INNER JOIN Orders ON Person.Id_P ...
- 基于slf4j的log4j实战
参考文档如下: http://blog.csdn.net/anialy/article/details/8529188 slf4j是接口,基于门面模式,可以实现log4j和logback 参考文档如下 ...
- HTML 5 Audio/Video DOM buffered 属性
1.实例1获取视频第一段缓冲范围部分,以秒计: myVid=document.getElementById("video1"); alert("Start: " ...
- CentOS 6.5 64位,调整分区大小
调整硬盘分区大小 想增加root空间,减少home空间. 1.查看硬盘使用情况. [root@npm ~]# df -h Filesystem Size Used Avail Use% Mounted ...
- asp.net mvc生命周期学习
ASP.NET MVC是一个扩展性非常强的框架,探究其生命周期对用Mock框架来模拟某些东西,达到单元测试效果,和开发扩展我们的程序是很好的. 生命周期1:创建routetable.把URL映射到ha ...
- 更新xcode后插件失效问题——不针对特定版本的通用解决方法
一.Xcode更新后插件失效的原理 1.每次更新Xcode后插件都会失效,其实插件都还在这个目录好好的躺着呢: ~/Library/Application Support/Developer/Shar ...
- C# 枚举
一.在学习枚举之前,首先来听听枚举的优点. 1.枚举能够使代码更加清晰,它允许使用描述性的名称表示整数值. 2.枚举使代码更易于维护,有助于确保给变量指定合法的.期望的值. 3.枚举使代码更易输入. ...