HDU2653 Waiting ten thousand years for Love (三维广搜+优先队列)
2016-03-17 10:31
453 查看
Waiting ten thousand years for Love
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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
思路:因为走到一个点时剩余的能量值会因为前面前进的方式不同而不同,所以要加一维表示当前坐标能量值的多少,再加上优先队列,进行广搜。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node{//存在优先队列中的结点
bool friend operator<(node a,node b){//重载运算符
return a.step>b.step;
}
int x,y,step,mofa;
};
char mp[82][82];
bool vis[82][82][82];//第三维是走到该点还剩余的能量值
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int n,m,t,p;
bool judge(int xx,int yy,int mofa){//判断能不能继续走的函数
if(xx>n||xx<1||yy>m||yy<1||mp[xx][yy]=='#')return 0;
if(mofa<0)return 0;
return 1;
}
int bfs(int stx,int sty){
priority_queue<node>q;//优先队列
node e1;
e1.x=stx;e1.y=sty;e1.step=0,e1.mofa=p;
q.push(e1);//起点入队列
vis[stx][sty][p]=1;
while(!q.empty()){
node e2=q.top();//优先队列要用top函数返回优先级最高的
q.pop();
if(e2.step>t)return -1;//如果当前步数最少的已经超过了t,就没必要继续搜了
if(mp[e2.x][e2.y]=='L')return e2.step;//找到终点,返回
for(int i=0;i<4;i++){
if(!judge(e2.x+dir[i][0],e2.y+dir[i][1],e2.mofa))continue;
if(e2.mofa>0&&!vis[e2.x+dir[i][0]][e2.y+dir[i][1]][e2.mofa-1]){//优先考虑飞的情况
node e3;
e3.x=e2.x+dir[i][0];
e3.y=e2.y+dir[i][1];
e3.step=e2.step+1;
e3.mofa=e2.mofa-1;
vis[e2.x+dir[i][0]][e2.y+dir[i][1]][e2.mofa-1]=1;
q.push(e3);
}
if(mp[e2.x][e2.y]!='@'&&mp[e2.x+dir[i][0]][e2.y+dir[i][1]]!='@'&&!vis[e2.x+dir[i][0]][e2.y+dir[i][1]][e2.mofa]){//走
node e3;
e3.x=e2.x+dir[i][0];
e3.y=e2.y+dir[i][1];
e3.step=e2.step+2;
e3.mofa=e2.mofa;
vis[e2.x+dir[i][0]][e2.y+dir[i][1]][e2.mofa]=1;
q.push(e3);
}
}
}
if(q.empty())return -1;
}
int main(){
int i,j,cases=1,stx,sty;
while(scanf("%d%d%d%d",&n,&m,&t,&p)!=EOF){
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++){
getchar();
for(j=1;j<=m;j++){
scanf("%c",&mp[i][j]);
if(mp[i][j]=='Y'){
stx=i;
sty=j;
}
}
}
int ans=bfs(stx,sty);
printf("Case %d:\n",cases++);
if(ans==-1||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 0;
}
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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.
思路:因为走到一个点时剩余的能量值会因为前面前进的方式不同而不同,所以要加一维表示当前坐标能量值的多少,再加上优先队列,进行广搜。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node{//存在优先队列中的结点
bool friend operator<(node a,node b){//重载运算符
return a.step>b.step;
}
int x,y,step,mofa;
};
char mp[82][82];
bool vis[82][82][82];//第三维是走到该点还剩余的能量值
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int n,m,t,p;
bool judge(int xx,int yy,int mofa){//判断能不能继续走的函数
if(xx>n||xx<1||yy>m||yy<1||mp[xx][yy]=='#')return 0;
if(mofa<0)return 0;
return 1;
}
int bfs(int stx,int sty){
priority_queue<node>q;//优先队列
node e1;
e1.x=stx;e1.y=sty;e1.step=0,e1.mofa=p;
q.push(e1);//起点入队列
vis[stx][sty][p]=1;
while(!q.empty()){
node e2=q.top();//优先队列要用top函数返回优先级最高的
q.pop();
if(e2.step>t)return -1;//如果当前步数最少的已经超过了t,就没必要继续搜了
if(mp[e2.x][e2.y]=='L')return e2.step;//找到终点,返回
for(int i=0;i<4;i++){
if(!judge(e2.x+dir[i][0],e2.y+dir[i][1],e2.mofa))continue;
if(e2.mofa>0&&!vis[e2.x+dir[i][0]][e2.y+dir[i][1]][e2.mofa-1]){//优先考虑飞的情况
node e3;
e3.x=e2.x+dir[i][0];
e3.y=e2.y+dir[i][1];
e3.step=e2.step+1;
e3.mofa=e2.mofa-1;
vis[e2.x+dir[i][0]][e2.y+dir[i][1]][e2.mofa-1]=1;
q.push(e3);
}
if(mp[e2.x][e2.y]!='@'&&mp[e2.x+dir[i][0]][e2.y+dir[i][1]]!='@'&&!vis[e2.x+dir[i][0]][e2.y+dir[i][1]][e2.mofa]){//走
node e3;
e3.x=e2.x+dir[i][0];
e3.y=e2.y+dir[i][1];
e3.step=e2.step+2;
e3.mofa=e2.mofa;
vis[e2.x+dir[i][0]][e2.y+dir[i][1]][e2.mofa]=1;
q.push(e3);
}
}
}
if(q.empty())return -1;
}
int main(){
int i,j,cases=1,stx,sty;
while(scanf("%d%d%d%d",&n,&m,&t,&p)!=EOF){
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++){
getchar();
for(j=1;j<=m;j++){
scanf("%c",&mp[i][j]);
if(mp[i][j]=='Y'){
stx=i;
sty=j;
}
}
}
int ans=bfs(stx,sty);
printf("Case %d:\n",cases++);
if(ans==-1||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 0;
}
相关文章推荐
- 优先队列(priority_queue)的C语言实现代码
- PHP 数据结构队列(SplQueue)和优先队列(SplPriorityQueue)简单使用实例
- 堆排序/优先级队列
- 优先队列<堆>
- 二叉堆
- vijosP1016北京2008的挂钟 BFS + 优先队列
- 优先队列的学习笔记
- 优先队列的学习笔记
- hdu 1596 find the safest road(spfa算法)
- Black Box
- 基于算法导论6.5用最大堆实现的优先队列(C++)
- Rescue
- Rescue
- 拯救公主
- 优先队列
- [C++ STL] 优先队列容器 priority_queue
- hdu2066 最短路 dijkstra
- hdu3790 dijkstra算法
- Fence Repair(简单贪心)
- 2015ACM多校对抗赛第四场 hdu 5335