题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1242

题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时。

解题思路

如果没有特殊点,就是普通BFS。

由于特殊点的介入,所以BFS树的同一深度,各个点的值可能不同。所以使用优先队列,先取出值小的搜。

搜到的第一个符合条件的结果肯定是最小的,break。

注意有多个起点,所以先记录下所有起点,依次BFS找最小。

  1. #include "cstdio"
  2. #include "string"
  3. #include "cstring"
  4. #include "iostream"
  5. #include "vector"
  6. #include "queue"
  7. using namespace std;
  8. #define inf 1<<28
  9. struct status
  10. {
  11. int x,y,dep;
  12. status(int x,int y,int dep):x(x),y(y),dep(dep) {}
  13. bool operator < (const status &a) const
  14. {
  15. return dep>a.dep;
  16. }
  17. };
  18. int n,m,map[][],vis[][],dir[][]={-,,,,,-,,},ans;
  19. void bfs(int x,int y)
  20. {
  21. priority_queue<status> Q;
  22. Q.push(status(x,y,));
  23. vis[x][y]=true;
  24. bool flag=false;
  25. while(!Q.empty())
  26. {
  27. if(flag) break;
  28. status t=Q.top();Q.pop();
  29. for(int s=;s<;s++)
  30. {
  31. int X=t.x+dir[s][],Y=t.y+dir[s][];
  32. if(vis[X][Y]||X<||X>n||Y<||Y>n||!map[X][Y]) continue;
  33. vis[X][Y]=true;
  34. if(map[X][Y]==) {flag=true;ans=min(ans,t.dep+);};
  35. if(map[X][Y]==) Q.push(status(X,Y,t.dep+));
  36. else Q.push(status(X,Y,t.dep+));
  37. }
  38. }
  39. }
  40. int main()
  41. {
  42. //freopen("in.txt","r",stdin);
  43. ios::sync_with_stdio(false);
  44. string tt;
  45. while(cin>>n>>m&&n)
  46. {
  47. vector<status> f;
  48. ans=inf;
  49. for(int i=;i<=n;i++)
  50. {
  51. cin>>tt;
  52. for(int j=;j<tt.size();j++)
  53. {
  54. if(tt[j]=='#') map[i][j+]=;
  55. if(tt[j]=='.') map[i][j+]=;
  56. if(tt[j]=='a') map[i][j+]=;
  57. if(tt[j]=='x') map[i][j+]=;
  58. if(tt[j]=='r') {map[i][j+]=;f.push_back(status(i,j+,));}
  59. }
  60. }
  61. for(int i=;i<f.size();i++)
  62. {
  63. memset(vis,,sizeof(vis));
  64. bfs(f[i].x,f[i].y);
  65. }
  66. if(ans!=inf) cout<<ans<<endl;
  67. else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
  68. }
  69. }
11869491 2014-10-14 14:50:56 Accepted 1242 31MS 612K 1861 B C++ Physcal

HDU 1242 (BFS搜索+优先队列)的更多相关文章

  1. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  2. HDU 2653 (记忆化BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...

  3. hdu 1242(BFS+优先队列)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. HDU 1242 Rescue(优先队列)

    题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description   Angel was caught by ...

  5. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  6. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  7. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

  8. HDU 1242——Rescue(优先队列)

    题意: 一个天使a被关在迷宫里,她的很多小伙伴r打算去救她.求小伙伴就到她须要的最小时间.在迷宫里有守卫.打败守卫须要一个单位时间.假设碰到守卫必须要杀死他 思路: 天使仅仅有一个,她的小伙伴有非常多 ...

  9. HDU 1242 rescue (优先队列模板题)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. 通过IIS调试ASP.NET项目

    当我们使用Visual Studio调试的时候,通常我们会选择VS自带的ASP.NET Developerment Server(也是默认选项),当第一次调试的时候(按F5或Ctrl+F5不调试直接打 ...

  2. MySQL的分页

    有朋友问: MySQL的分页似乎一直是个问题,有什么优化方法吗?网上看到网上推荐了一些分页方法,但似乎不太可行,你能点评一下吗? 方法1: 直接使用数据库提供的SQL语句 ---语句样式: MySQL ...

  3. 部署Office Web Apps Server并配置其与SharePoint 2013的集成

    部署Office Web Apps Server并配置其与SharePoint 2013的集成   Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.P ...

  4. iOS 中contraints居中对齐的一点心得

    今天遇到一个布局问题,先看图 如何让第二个控件在第一个和第三个的竖直上的中间呢?contraint不能直接实现这样的效果.我们可以再添加一个控件,如图 这样之后,再把第二个控件和这个新添加的控件设置为 ...

  5. 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 ...

  6. Java for LeetCode 029 Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  7. Eclipse设置C++自动补全变量名快捷键Alt + /

    使用快捷键:Alt+/ 要是还是有些场合不能提示,按照下列步骤 Window-Preferences-c/c++-Editor-Content Assist-Advanced 将未勾选的全部勾选

  8. Road Construction(poj 3352)

    题意:求最少天几条边,使这个无向图变成双连通图. /* tarjan缩点后,形成一棵树,求出叶子节点数tot,答案是(tot+1)/2 */ #include<cstdio> #inclu ...

  9. C++类的成员函数使用的一些小总结

    From: http://blog.csdn.net/xiayefanxing/article/details/7607506 这一阵做项目代码开发的时候,用到了在一个C++文件中使用另一个类的成员函 ...

  10. Android数据存储之sharedpreferences与Content Provider

    android中对数据操作包含有: file, sqlite3, Preferences, ContectResolver与ContentProvider前三种数据操作方式都只是针对本应用内数据,程序 ...