HDU1072:Nightmare [DFS]
题目链接:Nightmare
题意:
给出一张n*m的图,0代表墙,1代表可以走,2代表起始点,3代表终点,4代表炸弹重置点
问是否能从起点到达终点
分析:
一道很好的DFS题目,炸弹重置点必然最多走一次,可能会陷入无限递归,故有一个重要的剪枝,见代码,
此题采用记忆化搜索(不懂の),注意代码的设计思路
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int t,n,m,a[][],step[][],T[][],sx,sy,ex,ey,cnt,d[][]={,-,-,,,,,}; void dfs(int x,int y,int ste,int time)
{
if(time==) return ;
if(a[x][y]==) {cnt=min(ste,cnt);return ;}
if(a[x][y]==) time=;
if(ste>=step[x][y]&&time<=T[x][y]) return ;
step[x][y]=ste,T[x][y]=time;
for(int i=;i<;++i)
{
int xx=x+d[i][],yy=y+d[i][];
if(xx>n||yy>m||xx<||yy<||a[xx][yy]==) continue;
//if(step[x][y]<(step[xx][yy]-1)&&T[x][y]>(T[xx][yy]+1)) continue;
dfs(xx,yy,ste+,time-);
}
} int main()
{
for(scanf("%d",&t);t--;)
{
scanf("%d %d",&n,&m);
for(int i=;i<=n;++i)for(int j=;j<=m;++j)
{
scanf("%d",&a[i][j]);
if(a[i][j]==) sx=i,sy=j;
if(a[i][j]==) ex=i,ey=j;
step[i][j]=0x3f3f3f3f,T[i][j]=;
}
cnt=0x3f3f3f3f;
dfs(sx,sy,,);
if(cnt==0x3f3f3f3f) puts("-1");else printf("%d\n",cnt);
}
return ;
}
HDU1072:Nightmare [DFS]的更多相关文章
- HDU-1072 Nightmare (bfs+贪心)
Nightmare Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏
Nightmare Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth w ...
- HDU1072:Nightmare
传送门 题意 给出一张n*m的图 0.墙 1.可走之路 2.起始点 3.终点 4.时间重置点 问是否能到达终点 分析 我的训练专题第一题,一开始我设个vis数组记录,然后写炸,不能处理重置点根vis的 ...
- Nightmare(DFS)
Nightmare hdu1072 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- BFS、DFS ——J - Nightmare
J - Nightmare Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb ...
- hdu1072(Nightmare)bfs
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- Nightmare HDU1072
非常标准的BFS 第一次写错了很多 1.到达4时设置为墙就好了 避免了死循环 2.不用开d数组 在结构体里面就行了 3.结构体初始化函数的写法: Node(int x=0,int y=0,int ...
- CF453C Little Pony and Summer Sun Celebration (DFS)
http://codeforces.com/contest/456 CF454E Codeforces Round #259 (Div. 1) C Codeforces Round #259 (Di ...
- HDUOJ-----(1072)Nightmare(bfs)
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
随机推荐
- msp430项目编程17
msp430中项目---红外遥控系统 1.定时器工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习
- 洛谷 P3811 【模板】乘法逆元
P3811 [模板]乘法逆元 题目背景 这是一道模板题 题目描述 给定n,p求1~n中所有整数在模p意义下的乘法逆元. 输入输出格式 输入格式: 一行n,p 输出格式: n行,第i行表示i在模p意义下 ...
- 2003-07-16T01:24:32Z这是什么时间格式
这是标准的XML Schema的"日期型数据格式”. T是代表后面跟着“时间”.Z代表0时区,或者叫UTC统一时间. 世界的每个地区都有自己的本地时间,在Internet及无线电通信时,时间 ...
- 报错** is not accessible due to restriction on required library
报错: Description Resource Path Location TypeAccess restriction: The type Map<String,Object> is ...
- 相机标定(Camera calibration)
简单介绍 摄像机标定(Camera calibration)简单来说是从世界坐标系换到图像坐标系的过程.也就是求终于的投影矩阵 P 的过程,以下相关的部分主要參考UIUC的计算机视觉的课件(网址Spr ...
- Android消息机制1-Handler(Java层)(转)
转自:http://gityuan.com/2015/12/26/handler-message-framework/ 相关源码 framework/base/core/java/andorid/os ...
- 【Mongodb教程 第十三课 】PHP mongodb 的增删改查使用
<pre> <?php #phpinfo();die; #其他链接方式 #$conn=new Mongo(); #连接本地主机,默认端口. #$conn=new Mongo(&quo ...
- hdu 4902 Nice boat--2014 Multi-University Training Contest 4
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4902 Nice boat Time Limit: 30000/15000 MS (Java/Othe ...
- showModalDialog参数问题
showModalDialog传递参数: 1.参数拼接放在url中,参数过长或带特殊字符时,容易出现问题. 2.参数放在showModalDialog属性里 <script type=" ...
- java8--网络编程(java疯狂讲义3复习笔记)
重点复习一下网络通信和代理 java的网络通信很简单,服务器端通过ServerSocket建立监听,客户端通过Socket连接到指定服务器后,通信双方就可以通过IO流进行通信. 需要重点看的工具类:I ...