HDU2653 Waiting ten thousand years for Love (三维广搜+优先队列)
来源:互联网 发布:剑3萝莉捏脸数据下载 编辑:程序博客网 时间:2024/05/22 15:07
思路:因为走到一个点时剩余的能量值会因为前面前进的方式不同而不同,所以要加一维表示当前坐标能量值的多少,再加上优先队列,进行广搜。
代码如下:
#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;}
0 0
- HDU2653 Waiting ten thousand years for Love (三维广搜+优先队列)
- hdoj 2653 Waiting ten thousand years for Love(优先队列+BFS)
- Waiting ten thousand years for Love
- HDU 2653 Waiting ten thousand years for Love
- HDU 2653 Waiting ten thousand years for Love
- HDU 2653Waiting ten thousand years for Love
- Hdu 2579 Dating with girls(2) && hdu 2653 Waiting ten thousand years for Love【Bfs】
- HDU 2619 - Love you Ten thousand years (数论)
- hdu 2619 Love you Ten thousand years(数论,待解决)
- The Star Zoo——5、Waiting for fifteen thousand years
- POJ2251 Dungeon Master(三维广搜BFS+优先队列)
- 广搜+优先队列
- HDU1253 胜利大逃亡(三维广搜BFS+剪枝+优先队列)
- hdu1242 广搜+优先队列
- hdu1026 优先队列+广搜
- Hduoj1242【广搜+优先队列】
- Hduoj1026【广搜+优先队列】
- Rescue 优先队列+广搜
- Java8中新增加的集合类
- android AlertDialog的做法
- 工厂方法模式实现四则运算 C++
- java截取字符串 截串 substring
- SSL加密包解析的几个概念梳理
- HDU2653 Waiting ten thousand years for Love (三维广搜+优先队列)
- C#课后练习题(一)
- tomcat架构分析(valve源码导读)
- MD5 算法步骤详解
- HTML5延迟加载指定文件(google给的代码)
- 病毒!
- Redis 学习 ---- 1.引言
- 杭电ACM 1012 u Calculate e java
- problem1075 what are you talking about?