之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次。

那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间)。

为什么呢?我们重复走过一个点只有一个可能,那就是为了去取那个,所以如果取完后再回头经过这个点的时候剩余时间变多了,我们的目的就达到了。

left数组初值为0

优化:

重置时间的装置最多取一次就够了,所以可以再开一个标记数组vis记录装置是否用过。

 //#define LOCAL
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; struct Point
{
int x, y;
int steps;
int time;
}start, end; int map[][], vis[][], left[][];
int dir[][] = {{, }, {-, }, {, }, {, -}};
int row, col; void BFS(void)
{
queue<Point> qu;
start.time = , start.steps = ;
qu.push(start);
while(!qu.empty())
{
Point fir = qu.front();
qu.pop();
if(fir.time == ) continue;
if(fir.x == end.x && fir.y == end.y)
{
printf("%d\n", fir.steps);
return;
}
if(map[fir.x][fir.y] == )
fir.time = ;
for(int i = ; i < ; ++i)
{
int xx = fir.x + dir[i][];
int yy = fir.y + dir[i][];
if(xx< | xx>=row | yy< | yy>=col | (!map[xx][yy]))
continue;
Point next;
next.x = xx, next.y = yy;
next.time = fir.time - ;
next.steps = fir.steps + ;
if(map[xx][yy] == && vis[xx][yy])
continue;
if(next.time > left[xx][yy])
{
left[xx][yy] = next.time;
qu.push(next);
}
}
}
printf("-1\n");
} int main(void)
{
#ifdef LOCAL
freopen("1072in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &row, &col);
for(int i = ; i < row; ++i)
for(int j = ; j < col; ++j)
{
scanf("%d", &map[i][j]);
if(map[i][j] == )
start.x = i, start.y = j;
if(map[i][j] == )
end.x = i, end.y = j;
}
memset(vis, , sizeof(vis));
memset(left, , sizeof(left));
BFS();
}
return ;
}

代码君

HDU 1072 (不一样的入队条件) Nightmare的更多相关文章

  1. hdu 1072 Nightmare (bfs+优先队列)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...

  2. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  3. HDU 1072 Nightmare

    Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...

  4. HDU 1072 Nightmare (广搜)

    题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...

  5. HDU 1072 Nightmare 题解

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  6. HDU 1072(记忆化BFS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...

  7. hdu 1072(BFS) 有炸弹

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意是在一个n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器. 定 ...

  8. hdu - 1072(dfs剪枝或bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...

  9. hdu 3641 数论 二分求符合条件的最小值数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...

随机推荐

  1. 微信电脑版也能用公众号自定义菜单 微信1.2 for Windows发布

    昨日,微信电脑版发布更新,版本为微信1.2 for Windows,最大的特色就是加入了保存聊天记录功能,可以使用公账号菜单,手机上收藏的表情也能在电脑版上发送,可以接收转账消息. 本次微信pc版更新 ...

  2. 本地搭建Dubbo监控中心的安装步骤

    Dubbo监控中心的安装步骤 参考链接:http://blog.csdn.net/lichunan/article/details/40349645 一.从github上下载dubbo源码进行编译: ...

  3. Android Environment FAQ (Frequently Asked Question)

    1.how to find out the Eclipse Version From Eclipse Menu Help ----> About Eclipse It displayed as ...

  4. 转 wince程序 中使用Listview显示图标问题 (C#) .

    思路: 1.窗体控件:lstaqgl [Listview控件名称]  imageList1[ImageList控件] 2.  图片路径添加到—imageList1——Listview显示图片从 ima ...

  5. Activity学习(二)——生命周期

    一.什么是Activity? 简单的说:Activity就是布满整个窗口或者悬浮于其他窗口上的交互界面.在一个应用程序中通常由多个Activity构成,都会在Manifest.xml中指定一个主的Ac ...

  6. 学了C语言,如何利用cURL写一个程序验证某个网址的有效性?

    在<C程序设计伴侣>以及这几篇关于cURL的文章中,我们介绍了如何利用cURL写一个下载程序,从网络下载文件.可是当我们在用这个程序下载文件时,又遇到了新问题:如果这个网址是无效的,那么我 ...

  7. J-link V8固件升级记

    http://blog.sina.com.cn/s/blog_5bdee3020101khfy.html 好久没为电子工程事业尽份力了!今天也稍微努把力!写写我是如何升级J-link的固件的吧! J- ...

  8. Android开发:彻底更改工程名

    对于已经建立的工程,如果发现原来的工程名不合适,此时若想彻底更改工程名,需要三个步骤: 1.更改工程名 选中工程名,右键-->Refactor-->Rename. 2.更改src文件下包名 ...

  9. QTableWidget使用简单,因为不再存在父节点的关系

    虽然使用比较简单,但亲自过一遍还是有必要的,权当一个学习笔记吧,记录在此. #include "tablewidgetxxx.h" #include <QtGui/QAppl ...

  10. mongo中查询Array类型的字段中元素个数

    I have a MongoDB collection with documents in the following format: { "_id" : ObjectId(&qu ...