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多加一
代码
 #include<stdio.h>
#include<queue>
using namespace std;
struct Node{
    int nx,ny,step;
    friend bool operator < (Node a,Node b){
        return a.step>b.step;
    }
};
Node a,b;
char map[][];
int disx[]={,,-,};
int disy[]={,,,-};
int N,M,sx,sy,ex,ey,minstep,flot;
void bfs(){
    priority_queue<Node>dl;
    a.nx=sx;a.ny=sy;a.step=;
    dl.push(a);
    while(!dl.empty()){
        a=dl.top();
        dl.pop();
        map[a.nx][a.ny]='#';
        if(a.nx==ex&&a.ny==ey){flot=;
            minstep=a.step;
            return;
        }
        for(int i=;i<;i++){
            b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.step=a.step+;
            if(b.nx<||b.ny<||b.nx>=N||b.ny>=M||map[b.nx][b.ny]=='#')continue;
            if(map[b.nx][b.ny]=='.'||map[b.nx][b.ny]=='r')dl.push(b);
            else{
                b.step++;dl.push(b);
            }
        }
    }
}
int main(){
    while(~scanf("%d%d",&N,&M)){minstep=;flot=;
        for(int i=;i<N;i++)scanf("%s",map[i]);
        for(int x=;x<N;x++){
            for(int y=;y<M;y++){
                if(map[x][y]=='a')sx=x,sy=y;
                if(map[x][y]=='r')ex=x,ey=y;
            }
        }
        bfs();
        if(flot)printf("%d\n",minstep);
        else puts("Poor ANGEL has to stay in the prison all his life.");
    }
    return ;
}

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. poj1001(高精度)

                                                               Exponentiation Time Limit: 500MS   Memory ...

  2. 从设计模式说起JAVA I/O流

    从设计模式说起JAVA I/O流 之前写过一篇I/O流的入门介绍,直到最近看了设计模式,顺带理下I/O流的设计思路. JAVA类库中的I/O类分成输入和输出两部分,通过继承,任何自InputStrea ...

  3. [转]ActiveMQ 即时通讯服务 浅析

    一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provide ...

  4. poj 3411 Paid Roads(dfs)

    Description A network of m roads connects N cities (numbered to N). There may be more than one road ...

  5. 一个web初学者的笔记总结

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC" } p.p2 { margin: 0.0px ...

  6. Unity 之圆环算法

    首先我们要明白圆环生成的原理,其实说白了并不是圆环,而是圆.因为我们使用的预制物体时Cube(物体本身是有大小的)难免会有发生实物的折叠看起来给人的感觉是圆环而已. 1.1 几何中我们要画一个圆,因为 ...

  7. 【巧妙消维DP】【HDU2059】龟兔赛跑

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. centos 6 编译安装httpd-2.4

    centos6 yum安装的apr版本已经不适用httpd-2.4版本了.所以,需要源码编译apr以及apr-util 1. 下载源码: cd /usr/local/src/ wget http:// ...

  9. stagefright框架(四)-Video Buffer传输流程

    這篇文章將介紹Stagefright中是如何和OMX video decoder传送buffer. (1) OMXCodec會在一開始的時候透過read函式來傳送未解碼的data給decoder,並且 ...

  10. asp.net 常用的3中身份验证

    1. windows验证: IIS根据应用程序的设置来进行身份验证,要使用这中验证方式,必须禁止使用匿名用户登录. 2. Forms验证: 通过Cookies来保存用户凭证,对未登录的用户 重定向到自 ...