Rescue


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must
kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

Input

First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

Process to the end of the file.

Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL
has to stay in the prison all his life."

Sample Input

7 8 

#.#####. 

#.a#..r. 

#..#x... 

..#..#.# 

#...##.. 

.#...... 

........

Sample Output

13

这题是说Angle被人抓住了,她的朋友去救她。。而x是阻碍他的敌人,可是Angle的朋友足够强,能消灭全部敌人,可是在消灭敌人的同一时候,

他要多花费1秒的时间,题目要你求出他解救Angle所需的最短时间。。

这道题用BFS,但要注意的是最短路径也可能并非最优解。。

所以我们用定义一个二维数组来记录每一个位置时的最短时间,在BFS搜索的过程中,到某个位置时,搜索的结果大于最短时间时,不入队。

这样就攻克了本题的问题了。

此外,不要一推断到达目的位置时就推出BFS返回结果。。我就是由于在这里WA了一发。。由于这可能不是最优解。。仅仅有等到队列为空或

BFS搜索结束时,才干得出最优解或者是“无法到达目的位置”。

  1.  
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<vector>
  7. #include<queue>
  8. #include<cmath>
  9.  
  10. using namespace std;
  11.  
  12. const int maxn = 205;
  13. const int INF = 100000;//用于初始每一个位置的时间,尽可能开大点
  14. char map[maxn][maxn];//地图
  15. int mintime[maxn][maxn];//记录每一个位置的最小时间
  16. int n, m;//地图的大小
  17. int ax, ay;//Angel的位置
  18. int fx, fy;//Angel的friend的位置
  19. int ans;//结果
  20. //地图的走法
  21. int dx[] = { -1, 0, 0, 1 };
  22. int dy[] = { 0, -1, 1, 0 };
  23.  
  24. struct point
  25. {
  26. int x;
  27. int y;//方格的位置
  28. int time;//记录当前走到这所花费的时间
  29. };
  30. queue<point>Q;//队列
  31.  
  32. int BFS()//BFS函数
  33. {
  34. while( !Q.empty() )
  35. {
  36. point temp = Q.front();//记录队首元素
  37. Q.pop();
  38. // if( temp.x==ax && temp.y==ay )
  39. //return mintime[ax][ay]; 開始我在这输出,使得WA。。由于这可能不是最优解
  40. for(int i=0; i<4; i++)
  41. {
  42. int xx = temp.x + dx[i];
  43. int yy = temp.y + dy[i];
  44. if( xx>=0 && xx<n && yy>=0 && yy<m && map[xx][yy]!='#' )//排除边界以及墙壁
  45. {
  46. point next;//下一个位置
  47. next.x = xx;
  48. next.y = yy;
  49. next.time = temp.time + 1;
  50. if( map[xx][yy] == 'x' ) next.time += 1;//若遇到敌人,消灭其需加1
  51. if( next.time < mintime[xx][yy] )//若这样的走法所用的时间比原来mintime小则入队
  52. {
  53. mintime[xx][yy] = next.time;
  54. Q.push(next);
  55. }
  56. }
  57. }
  58. }
  59. return mintime[ax][ay];//返回结果
  60. }
  61.  
  62. void output()//输出
  63. {
  64. if( ans < INF )
  65. printf("%d\n", ans);
  66. else
  67. printf("Poor ANGEL has to stay in the prison all his life.\n");
  68. }
  69.  
  70. void init()//输入以及初始化
  71. {
  72. while(scanf("%d%d", &n, &m)==2)
  73. {
  74. memset( map, 0, sizeof(map) );
  75. for(int i=0; i<n; i++)
  76. scanf("%s", map[i]);//读入地图
  77. for(int i=0; i<n; i++)
  78. for(int j=0; j<m; j++)
  79. {
  80. mintime[i][j] = INF;//初始每一个位置的时间
  81. if( map[i][j] == 'r' ) //记录friend的位置
  82. {
  83. fx = i;
  84. fy=j;
  85. }
  86. if( map[i][j] == 'a' )//记录Angel的位置
  87. {
  88. ax = i;
  89. ay = j;
  90. }
  91. }
  92. point stay;
  93. stay.x = fx;
  94. stay.y = fy;
  95. stay.time = 0;
  96. mintime[fx][fy] = 0;
  97. Q.push( stay );//BFS的開始
  98. ans = BFS();
  99. output();
  100. }
  101. }
  102.  
  103. int main()
  104. {
  105. init();
  106.  
  107. return 0;
  108. }

ZOJ 1649:Rescue(BFS)的更多相关文章

  1. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

  2. 程序员的算法课(18)-常用的图算法:广度优先(BFS)

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...

  3. Rescue(bfs)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  4. ZOJ 1005:Jugs(思维)

    Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Har ...

  5. ZOJ 1091 Knight Moves(BFS)

    Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where you are t ...

  6. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

  7. SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...

  8. 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)

    参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...

  9. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

随机推荐

  1. centos安装和卸载软件

    ==如何卸载: 1.打开一个SHELL终端 2.因为Linux下的软件名都包括版本号,所以卸载前最好先确定这个软件的完整名称. 查找RPM包软件:rpm -qa ×××* 注意:×××指软件名称开头的 ...

  2. iOS8推送消息的回复处理速度

    iOS8我们有一个新的通知中心,我们有一个新的通报机制.当在屏幕的顶部仅需要接收一个推拉向下,你可以看到高速接口,天赋并不需要输入应用程序的操作.锁定屏幕,用于高速处理可以推动项目. 推送信息,再次提 ...

  3. Shell在大数据的魅力时代:从一点点思路百度大数据面试题

    供Linux开发中的同学们,Shell这可以说是一个基本功. 对于同学们的操作和维护.Shell也可以说是一种必要的技能,Shell.对于Release Team,软件配置管理的同学来说.Shell也 ...

  4. Oracle性能优化顺序表名称来选择最有效的学习笔记

    选择最有效的顺序表名(只有有效的基于规则的优化)  ORACLE分析器按照订单处理从右到左FROM在FROM子句中的表名,故FROM写在最后的表(基础表 driving table)将被最先处理. 在 ...

  5. 在 Swift 语言中更好的处理 JSON 数据:SwiftyJSON

    SwiftyJSON能够让在Swift语言中更加简便处理JSON数据. With SwiftyJSON all you have to do is: ? 1 2 3 4 let json = JSON ...

  6. Java设计模式菜鸟系列(两)建模与观察者模式的实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39755577 观察者(Observer)模式定义:在对象之间定义了一对多的依赖关系,这样一 ...

  7. VisualStudioOnline协同工作流程

    VisualStudioOnline协同工作流程 项目负责人登陆自己的vsonline新建项目就不多说了. 直接从邀请队友开始 项目负责人操作 被邀请的邮箱必须是微软的邮箱(也就是可以登录visual ...

  8. hadoop得知;block数据块;mapreduce实现样例;UnsupportedClassVersionError变态;该项目的源代码相关联

    对于开源的东西.特别是刚出来不久.我认为最好的学习方法是能够看到源代码,doc,样品测试 为了方便查看源代码,导入与项目相关的源代码 watermark/2/text/aHR0cDovL2Jsb2cu ...

  9. oracle查看经常使用的系统信息

    总结了查看oracle数据库的经常使用sql ,有助于分析优化出一个健壮的系统程序来. 1.当前的数据库连接数: select count(*) from v$process 2.数据库同意的最大连接 ...

  10. js cookie设置最大过期时间 Infinity

    Note: 对于永久cookie我们用了Fri, 31 Dec 9999 23:59:59 GMT作为过期日.如果你不想使用这个日期,可使用世界末日Tue, 19 Jan 2038 03:14:07 ...