您的位置:首页 > 大数据 > 人工智能

hdoj 2653 Waiting ten thousand years for Love(优先队列+BFS)

2014-04-15 18:27 281 查看

Waiting ten thousand years for Love

Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 809    Accepted Submission(s): 249

[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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.”
 

[align=left]Sample Input[/align]

2 3 2 2
Y@L
###
2 3 4 1
Y@L
###
2 3 4 0
Y.L
###
2 3 3 0
Y.L
###

 

[align=left]Sample Output[/align]

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.

HintHint
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.

 

[align=left]Author[/align]
lemon
 

[align=left]Source[/align]
决战龙虎门  

题目大意:一个地图,从起点到终点,起始有一个魔法值,每一次移动可以走也可以飞,走需要花费2s,飞需要花费1s及一个魔法值,‘#’不能走也不能飞,‘@’只能飞不能走,‘.'可以走也可以飞。求能否在时间t内到达。
解题思路:想到了用三维数组存状态,但没想到用优先队列。开始想用一个二维数组mincost

存每个点的最短时间,后来发现是错误的,,因为魔法值是不一样。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;
const int N = 100;
const int INF = 0x3fffffff;

struct Point{
int x, y;
int step;
int magic;
}start, fr, next, over;
char mp

;
int vis

;
int dir[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
int n, m, p, t;

bool operator<(Point x, Point y){
return x.step > y.step;
}

void bfs(){
priority_queue<Point> Q;
Q.push(start);
while(!Q.empty()){
fr = Q.top();
Q.pop();
if(fr.step > t) continue;
//  cout << fr.step << endl;
if(mp[fr.x][fr.y] == 'L'){
printf("Yes, Yifenfei will kill Lemon at %d sec.\n", fr.step);
return;
}
for(int i = 0; i < 4; i++){
next.x = fr.x + dir[i][0];
next.y = fr.y + dir[i][1];
if(mp[next.x][next.y] == '#' || mp[next.x][next.y] == '\0') continue;
if(mp[fr.x][fr.y] != '@' && mp[next.x][next.y] != '@' && !vis[next.x][next.y][fr.magic]){
next.magic = fr.magic;
next.step = fr.step + 2;
vis[next.x][next.y][next.magic] = 1;
Q.push(next);
}
if(fr.magic > 0 && !vis[next.x][next.y][fr.magic - 1]){
next.magic = fr.magic - 1;
next.step = fr.step + 1;
vis[next.x][next.y][next.magic] = 1;
Q.push(next);
}
}
}
puts("Poor Yifenfei, he has to wait another ten thousand years.");
return;
}

int main(){
int cas = 1;
while(scanf("%d%d%d%d", &n, &m, &t, &p) != EOF){
memset(mp, '\0', sizeof(mp));
for(int i = 1; i <= n; i++){
scanf("%s", mp[i] + 1);
for(int j = 1; j <= m; j++){
if(mp[i][j] == 'Y'){
start.x = i;
start.y = j;
start.magic = p;
}
else if(mp[i][j] == 'L'){
over.x = i;
over.y = j;
}
}
}
printf("Case %d:\n", cas++);
memset(vis, 0, sizeof(vis));
bfs();
}
return 0;
}

/*
1 5 10 5
Y@@@L
3 3 10 10
Y#.
@..
@.L
1 5 10 2
Y@@.L
1 5 10 2
L.@.Y
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: