Rescue

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 24   Accepted Submission(s) : 11
Problem Description
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
 
Author
CHEN, Xue
 题解:
没看见找不到还要输出字,错了n次,这就是没读清题就开始做题的后果。。。还有是遇见士兵要step多加一
代码
  1. #include<stdio.h>
  2. #include<queue>
  3. using namespace std;
  4. struct Node{
  5.     int nx,ny,step;
  6.     friend bool operator < (Node a,Node b){
  7.         return a.step>b.step;
  8.     }
  9. };
  10. Node a,b;
  11. char map[][];
  12. int disx[]={,,-,};
  13. int disy[]={,,,-};
  14. int N,M,sx,sy,ex,ey,minstep,flot;
  15. void bfs(){
  16.     priority_queue<Node>dl;
  17.     a.nx=sx;a.ny=sy;a.step=;
  18.     dl.push(a);
  19.     while(!dl.empty()){
  20.         a=dl.top();
  21.         dl.pop();
  22.         map[a.nx][a.ny]='#';
  23.         if(a.nx==ex&&a.ny==ey){flot=;
  24.             minstep=a.step;
  25.             return;
  26.         }
  27.         for(int i=;i<;i++){
  28.             b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.step=a.step+;
  29.             if(b.nx<||b.ny<||b.nx>=N||b.ny>=M||map[b.nx][b.ny]=='#')continue;
  30.             if(map[b.nx][b.ny]=='.'||map[b.nx][b.ny]=='r')dl.push(b);
  31.             else{
  32.                 b.step++;dl.push(b);
  33.             }
  34.         }
  35.     }
  36. }
  37. int main(){
  38.     while(~scanf("%d%d",&N,&M)){minstep=;flot=;
  39.         for(int i=;i<N;i++)scanf("%s",map[i]);
  40.         for(int x=;x<N;x++){
  41.             for(int y=;y<M;y++){
  42.                 if(map[x][y]=='a')sx=x,sy=y;
  43.                 if(map[x][y]=='r')ex=x,ey=y;
  44.             }
  45.         }
  46.         bfs();
  47.         if(flot)printf("%d\n",minstep);
  48.         else puts("Poor ANGEL has to stay in the prison all his life.");
  49.     }
  50.     return ;
  51. }

Rescue(bfs)的更多相关文章

  1. ZOJ 1649:Rescue(BFS)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

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

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

  3. hdu 1242 Rescue(bfs)

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

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

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

  5. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  6. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  7. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  8. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  9. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

随机推荐

  1. windows7 spark单机环境搭建及pycharm访问spark

    windows7 spark单机环境搭建 follow this link how to run apache spark on windows7 pycharm 访问本机 spark 安装py4j ...

  2. Impala 5、Impala 性能优化

    • 执行计划 – 查询sql执行之前,先对该sql做一个分析,列出需要完成这一项查询的详细方案 – 命令:explain sql.profile 要点: • 1.SQL优化,使用之前调用执行计划 • ...

  3. 也谈---基于 HTTP 长连接的“服务(转载)

    这里指讨论基于HTTP的推技术, 诸如flash,applet之类的东西不作分析, 他们就不能说是"纯粹"的浏览器应用了. 首先是一点背景知识, 大家都知道长连接避免了tcp连接的 ...

  4. 提高你的Java代码质量吧:小心switch带来的空值异常

    一.分析  使用枚举定义常量时,会有伴有大量的switch语句判断,目的是为每个枚举解释其行为. 我们知道,目前的Java的switch语句只能判断byte.short.char.int类型(JDK7 ...

  5. Swift中NSData与NSDictionary之间的相互转换

    原创Blog,转载请注明出处 使用NSKeyedUnarchiver类来进行相互转换 1.NSDictionary转NSData var dictionaryExample : [String:Any ...

  6. ThinkPHP使用Memcached缓存数据

    ThinkPHP默认使用文件缓存数据,支持Memcache等其他缓存方式,有两个PHP扩展:Memcache和Memcached,Memcahe官方有说明,主要说一下Memcached. 相对于PHP ...

  7. Ubuntu中nfs服务器安装与配置

    一.执行命令 sudo apt-get install nfs-kernel-server 二.为创建nfs文件夹 sudo mkdir /usr/nfs 更改目录权限:sudo chmod 777 ...

  8. mysql中,执行delete语句时出现Lock wait timeout exceeded问题

    问题描述: 当我插入一条记录时,在调用save方法的时候出现了异常(记录重复了),导致了后面的commit语句不能执行了.这时我在数据库中删除重复记录时发现该表已经被锁上了.即出现错误.但过了一会再次 ...

  9. 【Heritrix基础教程之2】Heritrix基本内容介绍

    1.版本说明 (1)最新版本:3.3.0 (2)最新release版本:3.2.0 (3)重要历史版本:1.14.4 3.1.0及之前的版本:http://sourceforge.net/projec ...

  10. JS数组删除一个元素(根据值删)

    <script type="text/javascript"> <!-- // 删除数组中第一个匹配的元素,成功则返回位置索引,失败则返回 -1. Array.p ...