HDU2653 BFS+优先队列
Waiting ten thousand years for Love
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1057 Accepted Submission(s): 335
was ten thousand years, after Demon Lemon caught Yifenfei’s love. In
order to revenge and save his love, Yifenfei have been practising sword
all day long and his Kongfu skills becomes so powerful that he can kill
Demon Lemon immediately. Recently, Yifenfei have found Lemon’s castle,
and now he is going to kill Lemon. At the same time, hearing about the
terrible news, Demon Lemon is now preparing for escaping...
Now Yifenfei has got the map of the castle.
Here are all symbols of the map:
Only one ‘Y’ Indicates the start position of Yifenfei.
Only one ‘L’ Indicates the position of Demon Lemon.
‘.’ Indicate the road that Yifenfei can walk on it, or fly over it.
‘#’ Indicate the wall that Yifenfei can not walk or flay through it.
‘@’ Indicate the trap that Yifenfei can not walk on it, but can fly over it.
Yifenfei
can walk or fly to one of up, down, left or right four directions each
step, walk costs him 2 seconds per step, fly costs him 1 second per step
and 1 magic power. His magic power will not increased, and if his magic
power is zero, he can not fly any more.
Now Yifenfei asks you
for helping him kill Demon Lemon smoothly. At the same time, Demon Lemon
will Leave the castle Atfer T seconds. If Yifenfei can’t kill Demon
Lemon this time, he have to wait another ten thousand years.
of test cases, please process to end of file. In each test case,
firstly will have four integers N, M, T, P(1 <= N, M, P <= 80, 1
<= T <= 100000), indicates the size of map N * M, the Lemon’s
leaving time(after T seconds, Lemon will disappeared) and Yifenfei’s
magic power. Then an N * M two-dimensional array follows indicates the
map.
each test case, first Print a line “Case C:”, C indicates the case
number. Then, if Yifenfei can kill Demon Lemon successfully, Print “Yes,
Yifenfei will kill Lemon at T sec.”, T indicates the minimum seconds he
must cost. Otherwise, Print ”Poor Yifenfei, he has to wait another ten
thousand years.”
Yes, Yifenfei will kill Lemon at 2 sec.
Poor Yifenfei, he has to wait another ten thousand years.
Yes, Yifenfei will kill Lemon at 4 sec.
Poor Yifenfei, he has to wait another ten thousand years.
Hint
Case 1: Yifenfei cost 1 second and 1 magic-power fly to ‘@’,
but he can not step on it, he must cost another 1 second
and 1 magic-power fly to ‘L’ and kill Lemon immediately.
Case 2: When Yifenfei Fly to ‘@’, he has no power to fly,
and is killed by trap.
//卡了很长时间,后来发现别人的题解vis是三维。
//显然要用到优先队列,本题的vis不是只访问一次,可以访问P次,因为以不同的能量值经过同一点的方案肯定是不同的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,t,p;
char map[][];
bool vis[][][];
int dir[][]={,,-,,,,,-};
struct Lu
{
int x,y,cnt,mag;
friend bool operator<(Lu a,Lu b)
{
return a.cnt>b.cnt;
}
};
int bfs(int sx,int sy)
{
priority_queue<Lu>q;
Lu L1,L2;
L1.x=sx;L1.y=sy;L1.cnt=;L1.mag=p;
vis[sx][sy][p]=;
q.push(L1);
while(!q.empty())
{
L2=q.top();
q.pop();
if(L2.cnt>t)
return ;
if(map[L2.x][L2.y]=='L')
return L2.cnt;
for(int i=;i<;i++)
{
L1.x=L2.x+dir[i][];
L1.y=L2.y+dir[i][];
if(L1.x<||L1.x>=n||L1.y<||L1.y>=m) continue;
if(map[L1.x][L1.y]=='#') continue;
if(L2.mag>&&!vis[L1.x][L1.y][L2.mag-])
{
L1.cnt=L2.cnt+;
L1.mag=L2.mag-;
vis[L1.x][L1.y][L1.mag]=;
q.push(L1);
}
if(map[L1.x][L1.y]!='@'&&map[L2.x][L2.y]!='@'&&!vis[L1.x][L1.y][L2.mag])
{
L1.cnt=L2.cnt+;
L1.mag=L2.mag;
vis[L1.x][L1.y][L2.mag]=;
q.push(L1);
}
}
}
return ;
}
int main()
{
int sx,sy,k=;
while(scanf("%d%d%d%d",&n,&m,&t,&p)!=EOF)
{
k++;
for(int i=;i<n;i++)
{
scanf("%s",map[i]);
for(int j=;j<m;j++)
if(map[i][j]=='Y')
{
sx=i;sy=j;
}
}
memset(vis,,sizeof(vis));
int ans=bfs(sx,sy);
printf("Case %d:\n",k);
if(ans>t) printf("Poor Yifenfei, he has to wait another ten thousand years.\n");
else printf("Yes, Yifenfei will kill Lemon at %d sec.\n",ans);
}
return ;
}
HDU2653 BFS+优先队列的更多相关文章
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- BFS+优先队列+状态压缩DP+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others) Memo ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- D. Lunar New Year and a Wander bfs+优先队列
D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...
随机推荐
- Windows平台下Qt中glut库的使用
用Qt中的QGLWidget窗体类中是不包括glut工具库的,难怪在myGLWidget(在我的程序中是QGLWidget的派生类)中绘制实心球体是说“glutSolidSphere”: 找不到标识符 ...
- JavaScript获取浏览器高度和宽度值(documentElement,clientHeight,offsetHeight,scrollHeight,scrollTop,offsetParent,offsetY,innerHeight)
IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.d ...
- JDK source 之 ArrayList 需要注意事项
线程安全 ArrayList内部没有实现原子性操作,所以是非线程安全的.如果需要在线程安全的环境下使用List的话,需要使用Vector 或者CopyOnWriteArrayList,具体场景,自行深 ...
- 【GWAS文献】基于GWAS与群体进化分析挖掘大豆相关基因
Resequencing 302 wild and cultivated accessions identifies genes related to domestication and improv ...
- oss文件删除策略
当你想删除oss服务中某个bucket下的文件夹时,文件夹中又包含了太多文件,递归删除太过耗时,又必须删除时,此时就要用oss的文件删除策略,如下所示: OSSClient client = new ...
- layer使用方法
type - 基本层类型 类型:Number,默认:0 layer提供了5种层类型.可传入的值有:0(信息框,默认)1(页面层)2(iframe层)3(加载层)4(tips层). 若你采用layer. ...
- myeclipse如何设置字体?
1.首先,将你的Myeclipse打开. 找到上面的菜单“windows”打开“Preferences” 2.然后,在弹出的设置窗口中找到“colors and fonts” 3.将右边的basic打 ...
- ORACLE简单触发器
因工作需要,尝试写一些建议的触发器,下面为其中一例. //创建一个名为TRG_FIN_OPB_ACCOUNTCARD的触发器 create or replace trigger TRG_FIN_OPB ...
- 动态选路、RIP协议&&OSPF协议详解
动态选路.RIP协议&&OSPF协议详解 概念 当相邻路由器之间进行通信,以告知对方每个路由器当前所连接的网络,这时就出现了动态选路.路由器之间必须采用选路协议进行通信,这样的选路协议 ...
- 解决NSData转NSString返回nil的问题
// 字符串转Data NSString *str =@"jesfds"; NSData *data =[str dataUsingEncoding:NSUTF8StringEnc ...