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 ...
随机推荐
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]
传送门 D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 me ...
- 【转载】js中对象的使用
原文链接:http://www.jb51.net/article/90256.htm[侵删] 简单记录javascript中对象的使用 一.创建对象 //创建一个空对象 var o={}; //创建一 ...
- 哀悼改变全站颜色为灰色CSS代码收藏
<style type="text/css"> *{ filter:progid:DXImageTransform.Microsoft.BasicIma ...
- Construct Binary Tree from Preorder and Inorder Traversal (DFS,参考)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- response对象学习
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...
- MongoDB学习day04--NodeJs操作数据库增删改查
一.在Nodejs中使用Mongodb Nodejs需要引入的包 npm install mongodb --save -dev 或者使用镜像 cnpm install mongodb --save ...
- Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class
这是版本的问题: 解决办法有两种: 1.降低Quartz版本,降到1.X去. 2.升级Spring版本到3.1+,根据Spring的建议,将原来的**TriggerBean替换成**TriggerFa ...
- js如何获取一个object的第一个数据
var obj = { "1":"123", "2":"456" } console.info( obj[Object. ...
- "What's New" WebPart in SharePoint
"What's New" WebPart in SharePoint 项目描写叙述 这是一个自己定义WebPart,能够显示一个列表,这个列表项目是在SharePo ...
- UI 07 _ 导航视图控制器 与 属性传值
首先, 先创建三个VC. 完毕点击按钮, 进入下一页, 并可以返回. 要先把导航视图控制器创建出来. 在AppDelegate.m 文件里代码例如以下: #import "AppDelega ...