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): 615 Accepted Submission(s): 205
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.
Y@L
###
2 3 4 1
Y@L
###
2 3 4 0
Y.L
###
2 3 3 0
Y.L
###
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
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<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=80+10;
char Map[MAX][MAX];
int mark[MAX][MAX][MAX];//在位置i,j剩余魔力k是否标记
int n,m,t,p;
int dir[4][2]={0,1,0,-1,1,0,-1,0}; struct Node{
int x,y,time,m;
Node(){}
Node(int X,int Y,int Time,int M):x(X),y(Y),time(Time),m(M){}
bool operator<(const Node &a)const{
return time>a.time;
}
}start; priority_queue<Node>q;
int BFS(int &flag){
while(!q.empty())q.pop();
Node next,oq;
q.push(start);
mark[start.x][start.y][start.m]=flag;
while(!q.empty()){
oq=q.top();
q.pop();
if(Map[oq.x][oq.y] == 'L')return oq.time;
if(oq.time>t)return INF;
for(int i=0;i<4;++i){
next=Node(oq.x+dir[i][0],oq.y+dir[i][1],oq.time+1,oq.m);
if(next.x<0 || next.y<0 || next.x>=n || next.y>=m)continue;
if(Map[next.x][next.y] == '#')continue;
if(Map[next.x][next.y] != '@' && Map[oq.x][oq.y] != '@' && mark[next.x][next.y][next.m] != flag){
++next.time;
q.push(next);
mark[next.x][next.y][next.m]=flag;
}
if(next.m>0 && mark[next.x][next.y][next.m-1] != flag){
--next.m;
next.time=oq.time+1;
q.push(next);
mark[next.x][next.y][next.m]=flag;
}
}
}
return INF;
} int main(){
int num=0;
while(cin>>n>>m>>t>>p){
for(int i=0;i<n;++i)cin>>Map[i];
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(Map[i][j] == 'Y')start.x=i,start.y=j;
}
}
start.time=0,start.m=p;
int temp=BFS(++num);
cout<<"Case "<<num<<":\n";
if(temp>t)cout<<"Poor Yifenfei, he has to wait another ten thousand years."<<endl;
else cout<<"Yes, Yifenfei will kill Lemon at "<<temp<<" sec."<<endl;
}
return 0;
}
hdu2653之BFS的更多相关文章
- HDU2653 BFS+优先队列
Waiting ten thousand years for Love Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/3 ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
随机推荐
- IDA Pro 权威指南学习笔记(十三) - 基本代码转换
IDA提供的代码转换包括: 1.将数据转换为代码 2.将代码转换为数据 3.指定一个指令序列为函数 4.更改现有函数的起始或结束地址 5.更改指令操作数的显示格式 代码显示选项 通过 Options ...
- Chrome Postman及Firefox Poster使用
Chrome浏览器跟Postman工具共用代理设置及Cookie Firefox浏览器跟Poster工具共用代理设置及Cookie xdebug调试原理 第一次请求url通过参数XDEBUG_SE ...
- web.xml执行顺序
出自:http://blog.csdn.net/u010833154/article/details/50697987 引言:启动java web程序的时候,java web会读取配置文件web.xm ...
- Android真机调试手动添加程序包的LogCat
android真机调试有时候看LogCat 时,有时候那个跑的本程序的LogCat 没有出现而是 出现的是" All messages (no filters) " .此时 的Lo ...
- Thread(线程)三
今天我们继续接着线程讲讲,上一章提到一下task概念, 首先接着task继续往下讲,在前章节提到过Thread怎么实现其他线程完成后再让主线程继续执行的功能,那么如果Task也需要线程等待事件,该怎么 ...
- SVN 与Git的区别
1:最主要的区别是Git是分布式版本控制系统,而SVN是集中式的版本控制系统.能理解这一点,区别它们就会容易很多,Git并不是目前唯一的分布式版本控制系统,比如还有Mercurial等.不过话说回来G ...
- Elasticsearch-PHP 概述
最近在学习使用Elasticsearch,并且是和PHP一起使用的,看到了Elasticsearch-PHP,其实是Elasticsearch为PHP提供的客户端,那么我们来学习一下API文档,如何在 ...
- centos7 修改静态ip 和dns
1.修改对应网卡的IP地址的配置文件# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 #描述网卡对应的设备别名,例如ifcfg-et ...
- java 蓝桥杯基础练习 01字串 进制转换
问题描述 对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能.它们的前几个是: 请按从小到大的顺序输出这32种01串. 输入格式 本试题没有输入. 输出格式 输出32行,按从小到大的顺 ...
- MonoBehaviour.print和Debug.Log是同样的作用
MonoBehaviour.print("identical------------------------");