hdu2488 dfs
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
解析见代码
代码:
/*
hdu2488 深搜,判断能否走完全图,并要求输出路径
首先是能否走完全图的判断,深搜函数加一个参数step,
来判断是否走完全图,同时用flag进行标记,方便输出两种情况
再就是路径如何保存,只需要保存每一步的x,y坐标即可,使用
一个二位组就可以保存。同时因为vis数组是以步数为标准来进行保存的
其值会随着递归回溯不断更新,始终保证是最新解,step=p*q标志着递归
成功,按照步数输出即可,注注意格式要求的是先输纵坐标后输横坐标
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
const int maxn=100;
int vis[maxn*maxn][2];//vis二维数组,前一个参数代表是第几步,后一个参数0代表横坐标,后一个参数代表纵坐标。
int p,q,step,flag;
char maps[maxn][maxn];
int f[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};//输出要求按字典序输出,同时注意大写字母是列编号,所以方向数组应该是按照先y后x字典序开
int ans=0;
int dis[maxn][maxn];
void dfs(int x,int y,int step)
{
if(step==p*q&&flag==0)
{
cout<<"Scenario #"<<++ans<<":"<<endl;
for(int i=0;i<p*q;i++)
printf("%c%d",'A'+vis[i][1],vis[i][0]+1);
flag=1;
cout<<endl<<endl;//输出格式要求
return;
}
for(int i=0;i<8;i++)
{
int a=x+f[i][0];
int b=y+f[i][1];
if(a>=0&&a<p&&b>=0&&b<q&&!dis[a][b])
{
dis[a][b]=1;
vis[step][0]=a;
vis[step][1]=b;
dfs(a,b,step+1);
dis[a][b]=0;//回溯时该点状态恢复
if(flag) return;//相当于一个剪枝操作,找到就返回,大大提高了程序工作效率
}
}
}
int main()
{
int n;
cin>>n;
while(n--)
{
memset(dis,0,sizeof(dis));
cin>>p>>q;
flag=0;
dis[0][0]=1;//标记数组,避免重复搜索
vis[0][0]=0,vis[0][1]=0;
dfs(0,0,1);
if(!flag)//用flag来判断最终是否走完全图
{
cout<<"Scenario #"<<++ans<<":"<<endl;
cout<<"impossible"<<endl<<endl;
}
}
return 0;
}
hdu2488 dfs的更多相关文章
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【BZOJ-1146】网络管理Network DFS序 + 带修主席树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3495 Solved: 1032[Submi ...
随机推荐
- 从零开始制作Minecraft启动器(C++开源)
从零开始制作Minecraft启动器(C++开源) 新手飙车了~~~,MC启动器源码大放送,随心所欲打造自己的专属MC启动器,这不是易语言,是C++...分析原理,关键源码都有详细的注释,代码编好就打 ...
- 1.1C++入门 未完待续。。。
第一个C++程序: #include<iostream> int main() { std::cout << "Hello World !" << ...
- poj1981 Circle and Points 单位圆覆盖问题
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Circle and Points Time Limit: 5000MS Me ...
- Guava API学习之Multimap
相信大家对Java中的Map类及其之类有大致的了解,Map类是以键值对的形式来存储元素(Key->Value),但是熟悉Map的人都知 道,Map中存储的Key是唯一的.什么意思呢?就是假如我们 ...
- 转载:css3 box-shadow投影发光效果
转载网址:http://www.wufangbo.com/css3-box-shadow/ CSS3的box-shadow属性 可以让我们轻松实现图层阴影效果.我们来实战详解一下这个属性. 1. bo ...
- GO函数倒叙输出
package main import "fmt" func main(){ rec() } func rec(i int){ { return } rec(i+) fmt.Pri ...
- Unity GUI TextField不能输入文字
最近在弄Unity的GUI. 也算是好久不用了,有点不熟悉了. 用TextField的时候发现GUI是出来了不过不能输入文字 到网上查了一下说要用一个public的string来接收 我看了我的代码 ...
- Linux下关闭node应用
今天在折腾用node接入微信公众号时,碰到了node应用启动后卡死退出,需要找出该进程关闭的问题,由于对shell脚本不是很熟悉,记录如下: 我们在用npm start启动应用后,通常要关闭时,ctr ...
- sql模糊匹配
执行 数据库查询时,有完整查询和模糊查询之分. 一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,%:表示任意0个 ...
- android 通过TimePickerDialog修改时间
初学android,写了个修改时间的小程序,实现如下: 点击change按钮,弹出时间对话框: 然后点击Done,设置显示时间: 实现方式有两种, 一种是使用showDialog方法,不过此种方法已过 ...