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

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

[email protected]

###

2 3 4 1

[email protected]

###

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点魔法值。

代码:

 1 //卡了很长时间,后来发现别人的题解vis是三维。
 2 //显然要用到优先队列,本题的vis不是只访问一次,可以访问P次,因为以不同的能量值经过同一点的方案肯定是不同的。
 3 #include<iostream>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<queue>
 7 using namespace std;
 8 int n,m,t,p;
 9 char map[82][82];
10 bool vis[82][82][82];
11 int dir[4][2]={1,0,-1,0,0,1,0,-1};
12 struct Lu
13 {
14     int x,y,cnt,mag;
15     friend bool operator<(Lu a,Lu b)
16     {
17         return a.cnt>b.cnt;
18     }
19 };
20 int bfs(int sx,int sy)
21 {
22     priority_queue<Lu>q;
23     Lu L1,L2;
24     L1.x=sx;L1.y=sy;L1.cnt=0;L1.mag=p;
25     vis[sx][sy][p]=1;
26     q.push(L1);
27     while(!q.empty())
28     {
29         L2=q.top();
30         q.pop();
31         if(L2.cnt>t)
32         return 10000000;
33         if(map[L2.x][L2.y]==‘L‘)
34         return L2.cnt;
35         for(int i=0;i<4;i++)
36         {
37             L1.x=L2.x+dir[i][0];
38             L1.y=L2.y+dir[i][1];
39             if(L1.x<0||L1.x>=n||L1.y<0||L1.y>=m) continue;
40             if(map[L1.x][L1.y]==‘#‘) continue;
41             if(L2.mag>0&&!vis[L1.x][L1.y][L2.mag-1])
42             {
43                 L1.cnt=L2.cnt+1;
44                 L1.mag=L2.mag-1;
45                 vis[L1.x][L1.y][L1.mag]=1;
46                 q.push(L1);
47             }
48             if(map[L1.x][L1.y]!=‘@‘&&map[L2.x][L2.y]!=‘@‘&&!vis[L1.x][L1.y][L2.mag])
49             {
50                 L1.cnt=L2.cnt+2;
51                 L1.mag=L2.mag;
52                 vis[L1.x][L1.y][L2.mag]=1;
53                 q.push(L1);
54             }
55         }
56     }
57     return 10000000;
58 }
59 int main()
60 {
61     int sx,sy,k=0;
62     while(scanf("%d%d%d%d",&n,&m,&t,&p)!=EOF)
63     {
64         k++;
65         for(int i=0;i<n;i++)
66         {
67             scanf("%s",map[i]);
68             for(int j=0;j<m;j++)
69             if(map[i][j]==‘Y‘)
70             {
71                 sx=i;sy=j;
72             }
73         }
74         memset(vis,0,sizeof(vis));
75         int ans=bfs(sx,sy);
76         printf("Case %d:\n",k);
77         if(ans>t) printf("Poor Yifenfei, he has to wait another ten thousand years.\n");
78         else printf("Yes, Yifenfei will kill Lemon at %d sec.\n",ans);
79     }
80     return 0;
81 }
时间: 2024-10-13 22:46:18

HDU2653 BFS+优先队列的相关文章

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

hdu 2102 A计划 详细题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 开始看到四分之一的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,不过在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间.果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,只不过多加了个参数,就是迷宫的层数,我用0代表第一层,1代表第二层,这在数组里面会体现的. struct node { int index;//层数

hdu 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an

poj 1724 ROADS (bfs+优先队列)

题目链接 题意:在有费用k限制的条件下,求从1到n的最短距离,如果最短距离相同求费用最小的,边为有向边,其中可能有 多个相同的源点和目标点,但是距离和费用不同. 分析:用bfs和邻接表来把每一个边搜一下,因为用了优先队列,所以先到n的一定是最小的 . 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio&

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,只是在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间. 果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,仅仅只是多加了个參数,就是迷宫的层数,我用0代表第一层.1代表第二层,这在数组里面会体现的. struct node { int index;

HDU-5025-Saving Tang Monk(BFS+优先队列)

Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujin