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

Problem Description
It
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.

 
Input
Lots
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.
 
Output
For
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.”
 
Sample Input
2 3 2 2
Y@L
###
2 3 4 1
Y@L
###
2 3 4 0
Y.L
###
2 3 3 0
Y.L
###
 
Sample Output
Case 1:
Yes, Yifenfei will kill Lemon at 2 sec.
Case 2:
Poor Yifenfei, he has to wait another ten thousand years.
Case 3:
Yes, Yifenfei will kill Lemon at 4 sec.
Case 4:
Poor Yifenfei, he has to wait another ten thousand years.

Hint

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.

 
Author
lemon
 
Source
 
题意:
n行m列的地图,开始位置Y,结束位置L,#代表墙不能走,@代表陷阱必须飞过去连续飞两下才能通过一个陷阱,.代表路可以走过去也可以飞过去,位在时间t内能否到达并求最短时间,飞过去用1秒,走过去用2秒,飞一次消耗1点魔法值,共有P点魔法值。
代码:
 //卡了很长时间,后来发现别人的题解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+优先队列的更多相关文章

  1. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  2. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  3. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  4. hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)

    题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...

  5. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  6. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  7. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  8. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  9. D. Lunar New Year and a Wander bfs+优先队列

    D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...

随机推荐

  1. bzoj1045 糖果传递

    escription 老师准备了一堆糖果, 恰好n个小朋友可以分到数目一样多的糖果. 老师要n个小朋友去拿糖果, 然后围着圆桌坐好, 第1个小朋友的左边是第n个小朋友, 其他第i个小朋友左边是第i-1 ...

  2. 转 C# 只允许运行一个实例

    来源:http://blog.csdn.net/jin20000/article/details/3136791 互斥进程(程序), 简单点说,就是在系统中只能有该程序的一个实例运行. 现在很多软件都 ...

  3. WSDL2java简单使用

    一.使用工具WSDL2java把接口转为本地可调用的.java文件 工具的目录结构: 设置WSDL2Java(URL).bat中的参数 set Axis_Lib=.\lib set Java_Cmd= ...

  4. Two Sum Leetcode Java

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  5. BZOJ 4576: [Usaco2016 Open]262144

    Description 一个序列,每次可以将两个相同的数合成一个数,价值+1,求最后最大价值 \(n \leqslant 262144\) Sol DP. 这道题是 BZOJ 4580: [Usaco ...

  6. 如何更高效地定制你的bootstrap

    bootstrap已经作为前端开发必不可少的框架之一,应用bootstrap使得我们对布局.样式的设定变得非常简单.但bootstrap提供的默认样式往往不能满足我们的需求,从而定制化bootstra ...

  7. java3

    1:在定义Long或者Float类型变量的时候,要加L或者f. 整数默认是int类型,浮点数默认是double. byte,short在定义的时候,他们接收的其实是一个int类型的值. 这个是自己做了 ...

  8. SpringMVC客户端发送json数据时报400错误

    当测试客户端发送json数据给服务器时,找不到响应路径? 原来是参数类型不符,即使是json也要考虑参数的个数和类型 解决:将age请求参数由"udf"改为"3" ...

  9. C语言笔记一

    学习C语言已经有一段时间,然而发现越学不知道的东西越多,这是在印象笔记中记得一些东西,现在再回顾一遍顺便补充一些新东西. 一,基础知识 运算符号  优先级  单目>算术>关系 从高到低   ...

  10. DTD文档模型和HTML基础

    html是超文本标记语言,现在常用到的2中文档格式是html5和XHTML 1.0 Transitiona(过渡). <!DOCTYPE html> <!--当前文档为html5-- ...