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 ...
随机推荐
- GRADLE 构建最佳实践
随着谷歌对Eclipse的无情抛弃和对Android Studio的日趋完善,使用gradle构建Android项目已经成为开发者的一项必会良技.那么,问题来了,采用什么样的姿势才能让项目开发构建过程 ...
- 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高
第十一章 搭建云端服务器 该章主要介绍了移动后端服务的概念以及Bmob的使用,比较简单,所以略过不总结. 第十三章 Android实例提高 该章主要介绍了拼图游戏和2048的小项目实例,主要是代码,所 ...
- android开发之Notification学习笔记
今天总结了一下Notification的使用,与大家分享一下. MainActivity.java: 本文参考:http://www.jb51.net/article/36567.htm,http:/ ...
- js 高阶函数 map reduce
map() var arr = [1,3,4]; function a(x){ return x*x; } //map可以将一个函数作为参数执行,将数组中的值,依次使用a函数处理: return ar ...
- 23、Javascript DOM
DOM Document Object Model(文档对象模型)定义了html和xml的文档标准. DOM 节点树 <html> <head> <title>DO ...
- js的MVC结构设计
基于jquery的Deferred,设计出如下MVC架构. 模型model interface.js interface: function(userid){ var dtd = $.Deferred ...
- OSI七层模型理解
物理层功能1,为数据端设备提供传送数据的通路 功能2,传输数据 接口.传输介质.信号的传输.网络设备 有线介质:双绞线(普通的网线),光纤. 无线介质:无线电.微波.激光.红外线. 例如手机.电视接收 ...
- 为Angular-UEditor增加工具栏属性
感谢胡大大分享的的开源项目 Angular 的 UEditor 插件 Angular-UEditor 本文只是修改了angular-ueditor.js,加入了对工具栏的定制,方便项目使用 1 (fu ...
- 【BZOJ2752】【线段树】高速公路
Description Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站. Y901高速公路是一条由N-1段路以及N个 ...
- VS2010 release 和 debug 调试区别
VC下Debug和Release区别 最近写代码过程中,发现 Debug 下运行正常,Release 下就会出现问题,百思不得其解,而Release 下又无法进行调试,于是只能采用printf方式逐步 ...