题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072

题目描述:矩阵表示迷宫,0表示墙,1表示路,2表示起点,3表示终点,4表示重置炸弹时间(6秒),你需要从起点出发(炸弹初始为6秒),在炸弹爆炸前到达终点,问最少需要多少时间。

分析:dfs,可以走走过的路,所以不能使用vis数组来标记,而是用dis和tim两个数组来帮助判断所走路线,通过这两个数组的记录来剪枝(此处剪枝:当sum大于dis[x][y]且time<=tim[x][y]说明当前这条路并非最佳路线,此处着重理解)。

总结:dfs摸到的些许门道,还需加强,剪枝还需要多见识一些。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define INF 100
int map[][];
int dir[][]= {{-,},{,},{,},{,-}};
int dis[][];
int tim[][];
int n,m,minx; bool inside(int x,int y)
{
if(x<n&&x>=&&y<m&&y>=)
return ;
return ;
} void dfs(int x,int y,int time,int sum)
{
if(map[x][y]==&&time>)
{
if(sum<minx)
minx=sum;
return;
}
if(time<=)
return;
if(map[x][y]==)
return;
if(!inside(x,y))
return;
if(time<=tim[x][y]&&sum>=dis[x][y]) //剪枝
return;
if(map[x][y]==)
time=;
dis[x][y]=sum;
tim[x][y]=time;
for(int i=; i<; i++)
{
int mx=x+dir[i][];
int my=y+dir[i][]; dfs(mx,my,time-1,sum+1);
}
} int main()
{
int t,sx,sy,ex,ey;
scanf("%d",&t);
while(t--)
{
memset(tim,,sizeof(tim));
minx=INF;
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
for(int j=; j<m; j++)
dis[i][j]=INF;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==)
{
sx=i;
sy=j;
}
}
dfs(sx,sy,,);
if(minx!=INF)
printf("%d\n",minx);
else
printf("-1\n");
}
return ;
}

HDU_1072_Nightmare的更多相关文章

  1. hdu_1072_Nightmare(BFS)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题意:给你一个地图,让你在炸弹爆之前找到出口,最初炸弹设定为6,每走一格需要1,中途有地方能让炸 ...

随机推荐

  1. C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题

    C#字符串数组排序   //排序只带字符的数组,不带数字的 private   string[]   aa   ={ "a ", "c ", "b & ...

  2. 一张图告诉你是须要 SQL 还是 Hadoop

    译序 非常多朋友问时下如火如荼的 Hadoop 是否适合引进我们自己的项目,什么时候用 SQL.什么时候用 Hadoop,它们之间怎样取舍?Aaron Cordova 用一张图来回答你这个问题,对于不 ...

  3. Mac SavePanel 保存文件的GUI代码

    // Move the recorded temporary file to a user-specified location (视频文件另存储过程,依据用户选择的路径和文件保存名) NSSaveP ...

  4. c# DataGridView样式设置无效

    对DataGridView中的某些行设置样式时,无效,最后发现,我是先设置完样式再进行展现的this.controls.Add,应该先展现完了,再设置样式.

  5. https://security.stackexchange.com/questions/68405/what-is-tmunblock-cgi-and-can-it-be-exploited-by-shellshock-linux-apache-w

    hndUnblock.cgi   Line #1124 : 187.38.233.45 - - [15/Jan/2018:21:36:45 +0800] "GET /hndUnblock.c ...

  6. Bing必应地图中国API入门讲座之八:显示驾车路线

    Bing必应地图中国API入门讲座之八:显示驾车路线 2011-05-24 14:47:36|  分类: Bing&Google|字号 订阅     这篇文章非常值得纪念,因为我是在Googl ...

  7. HTTPS数据包抓取的可行性分析

    HTTPS数据包抓取的可行性分析 相信只要是从事软件开发, 多多少少都会涉及到数据包的抓取.常见的有网页数据抓取(即网页爬虫),应用程序数据包抓取等.网页数据抓取比较简单, 在chrome下可以非常方 ...

  8. Python: PS 滤镜--马赛克

    本文利用 Python 实现PS 滤镜中的马赛克效果,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/details/30 ...

  9. the odb manual

    http://www.codesynthesis.com/products/odb/doc/manual.xhtml#18.4

  10. bzoj3884

    http://www.lydsy.com/JudgeOnline/problem.php?id=3884 拓展欧拉定理 http://blog.csdn.net/Pedro_Lee/article/d ...