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 ...
随机推荐
- 通过IIS调试ASP.NET项目
当我们使用Visual Studio调试的时候,通常我们会选择VS自带的ASP.NET Developerment Server(也是默认选项),当第一次调试的时候(按F5或Ctrl+F5不调试直接打 ...
- MySQL的分页
有朋友问: MySQL的分页似乎一直是个问题,有什么优化方法吗?网上看到网上推荐了一些分页方法,但似乎不太可行,你能点评一下吗? 方法1: 直接使用数据库提供的SQL语句 ---语句样式: MySQL ...
- 部署Office Web Apps Server并配置其与SharePoint 2013的集成
部署Office Web Apps Server并配置其与SharePoint 2013的集成 Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.P ...
- iOS 中contraints居中对齐的一点心得
今天遇到一个布局问题,先看图 如何让第二个控件在第一个和第三个的竖直上的中间呢?contraint不能直接实现这样的效果.我们可以再添加一个控件,如图 这样之后,再把第二个控件和这个新添加的控件设置为 ...
- Java for LeetCode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Eclipse设置C++自动补全变量名快捷键Alt + /
使用快捷键:Alt+/ 要是还是有些场合不能提示,按照下列步骤 Window-Preferences-c/c++-Editor-Content Assist-Advanced 将未勾选的全部勾选
- Road Construction(poj 3352)
题意:求最少天几条边,使这个无向图变成双连通图. /* tarjan缩点后,形成一棵树,求出叶子节点数tot,答案是(tot+1)/2 */ #include<cstdio> #inclu ...
- C++类的成员函数使用的一些小总结
From: http://blog.csdn.net/xiayefanxing/article/details/7607506 这一阵做项目代码开发的时候,用到了在一个C++文件中使用另一个类的成员函 ...
- Android数据存储之sharedpreferences与Content Provider
android中对数据操作包含有: file, sqlite3, Preferences, ContectResolver与ContentProvider前三种数据操作方式都只是针对本应用内数据,程序 ...