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 ...
随机推荐
- [codeforces 528]A. Glass Carving
[codeforces 528]A. Glass Carving 试题描述 Leonid wants to become a glass carver (the person who creates ...
- Linux 高可用开源方案 Keepalived VS Heartbeat对比
1)Keepalived使用更简单:从安装.配置.使用.维护等角度上对比,Keepalived都比Heartbeat要简单得多,尤其是Heartbeat2.1.4后拆分成3个子项目,安装.配置.使用都 ...
- order by 指定顺序 mysql
LOCATE(substr,str), LOCATE(substr,str,pos) 第一个语法返回字符串str第一次出现的子串SUBSTR的位置.第二个语法返回第一次出现在字符串str的子串SUBS ...
- 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定
aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...
- yum缓存配置
引自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/10/09/2203916.html $ cat /etc/yum.conf ...
- 3.子数组的最大和[MaximumContinuousSubArray]
[题目]: 输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值,要求时间复杂度为O(n). 例如输入的数组为1, -2, ...
- windows下批量删除文件
FORFILES /P d:\www /D -7 /S /M ex*.log /C "cmd /c del @path" 删除d:\www目录下7天前ex*.log的所有文件 例子 ...
- Greedy:Stall Reservations(POJ 3190)
牛挤奶 题目大意:一群牛很挑剔,他们仅在一个时间段内挤奶,而且只能在一个棚里面挤,不能与其他牛共享地方,现在给你一群牛,问你如果要全部牛都挤奶,至少需要多少牛棚? 这一题如果把时间区间去掉,那就变成装 ...
- JavaScript当离开页面时可以进行的操作
当JavaScript离开页面时可以进行的操作 window.onbeforeunload = function() { var email = document.getElementById(&qu ...
- Android activity_main.xml删除边缘距离,充满屏幕
删除android:paddingBottom.android:paddingLeft.android:paddingRight和android:paddingTop四个属性