HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242
题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时。
解题思路:
如果没有特殊点,就是普通BFS。
由于特殊点的介入,所以BFS树的同一深度,各个点的值可能不同。所以使用优先队列,先取出值小的搜。
搜到的第一个符合条件的结果肯定是最小的,break。
注意有多个起点,所以先记录下所有起点,依次BFS找最小。
#include "cstdio"
#include "string"
#include "cstring"
#include "iostream"
#include "vector"
#include "queue"
using namespace std;
#define inf 1<<28
struct status
{
int x,y,dep;
status(int x,int y,int dep):x(x),y(y),dep(dep) {}
bool operator < (const status &a) const
{
return dep>a.dep;
}
};
int n,m,map[][],vis[][],dir[][]={-,,,,,-,,},ans;
void bfs(int x,int y)
{
priority_queue<status> Q;
Q.push(status(x,y,));
vis[x][y]=true;
bool flag=false;
while(!Q.empty())
{
if(flag) break;
status t=Q.top();Q.pop();
for(int s=;s<;s++)
{
int X=t.x+dir[s][],Y=t.y+dir[s][];
if(vis[X][Y]||X<||X>n||Y<||Y>n||!map[X][Y]) continue;
vis[X][Y]=true;
if(map[X][Y]==) {flag=true;ans=min(ans,t.dep+);};
if(map[X][Y]==) Q.push(status(X,Y,t.dep+));
else Q.push(status(X,Y,t.dep+));
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
string tt;
while(cin>>n>>m&&n)
{
vector<status> f;
ans=inf;
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
if(tt[j]=='#') map[i][j+]=;
if(tt[j]=='.') map[i][j+]=;
if(tt[j]=='a') map[i][j+]=;
if(tt[j]=='x') map[i][j+]=;
if(tt[j]=='r') {map[i][j+]=;f.push_back(status(i,j+,));}
}
}
for(int i=;i<f.size();i++)
{
memset(vis,,sizeof(vis));
bfs(f[i].x,f[i].y);
}
if(ans!=inf) cout<<ans<<endl;
else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
}
| 11869491 | 2014-10-14 14:50:56 | Accepted | 1242 | 31MS | 612K | 1861 B | C++ | Physcal |
HDU 1242 (BFS搜索+优先队列)的更多相关文章
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- HDU 2653 (记忆化BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...
- hdu 1242(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1242 Rescue(优先队列)
题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- HDU 1242——Rescue(优先队列)
题意: 一个天使a被关在迷宫里,她的很多小伙伴r打算去救她.求小伙伴就到她须要的最小时间.在迷宫里有守卫.打败守卫须要一个单位时间.假设碰到守卫必须要杀死他 思路: 天使仅仅有一个,她的小伙伴有非常多 ...
- HDU 1242 rescue (优先队列模板题)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
随机推荐
- cmake编译dcmtk,并利用vs2010 进行开发mfc 程序
这几天要处理 医学图像数据,经同学推荐 采用 dcmtk 关于 编译 dcmtk 是可参考如下blog 1. http://blog.csdn.net/okaimee/article/details/ ...
- Shell编程基础
写之前我们先来搞清楚为什么要学shell,学习要有目的性shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题shell可以实现自动化管理,让系统管理员的工作变得容易.简单.高效shell脚 ...
- Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- Objective-C ,C++,java中常用编码格式对比
这个题目可能不太对!主要总结一下这3种语言的不同格式 1.创建一个A类,继承B类,实现C接口(协议) 先看oc的代码 @interface A : B <C> { int a; } @pr ...
- springJDBC实现查询方法二
无废话,看代码: @Override public List<Sites> queryAllSites(Pager pager) { String sql = "select * ...
- ORACLE查询某一字段重复的数据
第一种方法: select a.* from ASSET_MAINTAIN a inner join ASSET_MAINTAIN b on a.asset_id=b.asset_id and a. ...
- 【python】在python中调用mysql
资料:http://www.runoob.com/python/python-mysql.html 例子: 假设有数据路HTMLHASH, 其中有表htmlhash, 表中包括两个varchar(10 ...
- 【python】继承时注意事项
1. __init__ 注意事项 如果父类有__init__函数,子类没有,则子类自动调用父类__init__函数 如果父类有__init__函数,子类也有,则子类必须主动调用父类__init__函数 ...
- PO/VO/BO等对象模型
PO :persistent object持久对象 1 .有时也被称为Data对象,对应数据库中的entity,可以简单认为一个PO对应数据库中的一条记录. 2 .在hibernate持久化框架中与i ...
- 如何手动修改XP系统属性中的技术支持信息
\windows\system32目录下有个oeminof.ini,里面是OEM显示的文字信息,把相应项目修改即可,OEM图片使用的是本目录下的OEMlogo.bmp(图片:创建一个图形文件,像素尺寸 ...