HDU 2653 (记忆化BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653
题目大意:迷宫中有普通点和陷阱。其中普通点可以走可以飞,但是陷阱只能飞。走耗时1,飞耗时2。但是飞耗能1。给定一定能量P,问是否能在T秒内走出。
解题思路:
一开始SB似地认为每个点最多访问两次。其实每个点最多可以访问P次。
vis[X][Y][P]表示在(x,y)点能量为P的状态。
容易出错的地方在于这个组合: @. ,虽说是飞吧,但是还是会在陷阱上卡1s,尽管下一个点是. ,但是这种情况是必须飞的。
@@肯定是飞的,..这个就是可飞,可不飞。
BFS树明显是不均衡的,使用优先队列找到的第一个答案就可以return。
#include "cstdio"
#include "string"
#include "cstring"
#include "iostream"
#include "queue"
using namespace std;
char map[][];
int n,m,T,p,no,sx,sy,ex,ey,vis[][][],dir[][]={-,,,,,-,,};
struct status
{
int x,y,dep,mana;
status(int x,int y,int dep,int mana):x(x),y(y),dep(dep),mana(mana) {}
bool operator < (const status &a) const {return dep > a.dep;}
};
int bfs(int x,int y,int mana)
{
priority_queue<status> Q;
Q.push(status(x,y,,mana));
vis[x][y][mana]=;
while(!Q.empty())
{
status t=Q.top();Q.pop();
if(t.dep>=T) return -;
for(int s=;s<;s++)
{
int X=t.x+dir[s][],Y=t.y+dir[s][];
if(X<||X>n||Y<||Y>m||map[X][Y]=='#') continue;
if(map[X][Y]=='@')
{
if(t.mana<||vis[X][Y][t.mana-]) continue;
vis[X][Y][t.mana-]=true;
Q.push(status(X,Y,t.dep+,t.mana-));
}
else
{
if(map[t.x][t.y]=='@'&&t.mana<) continue;
if(X==ex&&Y==ey)
{
if(t.mana>=) return t.dep+;
else return t.dep+;
}
if(t.mana>=||map[t.x][t.y]=='@')
{
if(!vis[X][Y][t.mana-]) {vis[X][Y][t.mana-]=true;Q.push(status(X,Y,t.dep+,t.mana-));}
}
if(map[t.x][t.y]!='@'&&!vis[X][Y][t.mana]) {vis[X][Y][t.mana]=true;Q.push(status(X,Y,t.dep+,t.mana));}
}
}
}
return -;
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
string tt;
while(cin>>n>>m>>T>>p)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
map[i][j+]=tt[j];
if(tt[j]=='Y') {sx=i;sy=j+;}
if(tt[j]=='L') {ex=i;ey=j+;}
}
}
int ans=bfs(sx,sy,p);
cout<<"Case "<<++no<<":"<<endl;
if(ans>T||ans==-) cout<<"Poor Yifenfei, he has to wait another ten thousand years."<<endl;
else cout<<"Yes, Yifenfei will kill Lemon at "<<ans<<" sec."<<endl;
}
}
| 11892623 | 2014-10-17 12:22:29 | Accepted | 2653 | 218MS | 2956K | 2378 B | C++ | Physcal |
HDU 2653 (记忆化BFS搜索+优先队列)的更多相关文章
- HDU 2364 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...
- HDU 2579 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有 ...
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
- hdu 4722(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. #include<iostre ...
- HDU 4597 记忆化搜索
² 博弈取牌—记忆化搜索 题目描述: 有两副带有数字的牌,(数字>0)两人轮流取,取中了某张牌,自己的分数就加上牌上的数字,但只能从两端取,每人都会用最优的策略使得自己的分数最高.问A先取,他能 ...
- hdu 1514 记忆化搜索
题意是给4堆(堆的高度小于等于40)有颜色(颜色的种类小于等于20)的物品,你有一个篮子最多能装5件物品,每次从这4堆物品里面任取一件物品放进篮子里,但是取每堆物品时,必须先取上面的物品,才能取下面的 ...
- hdu 1208 记忆化搜索
题目大意:只能按照格子上的数字*方向走,从左上走到右下Sample Input42331121312313110Sample Output3 直接记忆化搜索,注意是0的情况 #include<c ...
- hdu 4960 记忆化搜索 DP
Another OCD Patient Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Ot ...
- hdu 2089 记忆化搜索写法(数位dp)
/* 记忆化搜索,第二维判断是否是6 */ #include<stdio.h> #include<string.h> #define N 9 int dp[N][2],digi ...
随机推荐
- HDU1711
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【Spring】Spring系列3之Spring AOP
3.Spring AOP 3.1.AOP概述 3.2.前置通知 3.3.后置通知 3.4.返回通知.异常通知.环绕通知 3.5.指定切面优先级 3.6.重用切入点表达式 3.7.引入通知 3.8.基于 ...
- 【转】如何修改Chrome缓存目录的地址
本文转自:http://www.nowamagic.net/librarys/veda/detail/2573 C盘空间越来越小,在Win7里还标红了,心里看得不舒服,得想一些方法腾出一些空间.看了A ...
- MySQL自带information_schema数据库使用
MySQL的information_schema数据库是什么,有什么作用? 大家在安装或使用MYSQL时,会发现除了自己安装的数据库以外,还有一个 information_schema数据库.info ...
- BestCoder9 1003 Revenge of kNN(hdu 4995) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4995 题目意思:在一个一维坐标轴上,给出位置 xi 和值 vi,对于 M 次询问,每次询问给出inde ...
- JsRender语法
{{:#data.Name}} 或 {{:Name}} 直接显示html格式{{>#data.Name}} 或 {{>Name}} 转义显示html字符 if else {{if bool ...
- SQL 删除存在于A表但是不存在B表中的记录
目的是是的A表和B表某一个列集合相等 delete from A where tagetColumn not in ( select targetColumn from B)
- [Linux] 孤儿进程与僵尸进程[总结]
转载: http://www.cnblogs.com/Anker/p/3271773.html 1.前言 之前在看<unix环境高级编程>第八章进程时候,提到孤儿进程和僵尸进程,一直对这两 ...
- 一、HTML和CSS基础--HTML+CSS基础课程--第5部分
第九章 CSS盒模型 元素分类 : 在讲解CSS布局之前,我们需要提前知道一些知识,在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 常用的 ...
- redis 认证密码
[root@cache01 ~]# grep "requirepass" /app/server/redis/conf/6379.conf # If the master is p ...