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. poj 1986 Distance Queries(LCA)

    Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...

  2. 格而知之8:我所理解的Runtime(3)

    关联对象 14.使用Category对类进行拓展的时候,只能添加方法,而不适合添加属性(可以添加属性,也可以正常使用get方法和set方法,只是不会自动生成以下划线开头命名的成员变量). 可以通过关联 ...

  3. ubuntu 文件readonly的问题: W10: Warning: Changing a readonly file 解决办法

    日前,笔者遇到一个奇怪且让人蛋疼的问题,借用别人的话"大家在linux上编辑文件的时候,明明是使用的root登录的,可是这种至高无上的权限在按下i的时候被那串红色错误亵渎了W10: Warn ...

  4. react-native 环境配置及hello world

    一.前言 最近手头的工作繁多,有研究性的项目和系统研发,正好遇到同事离职,接手了框架的UI组件,不仅需要维护和填坑,还需要开发新的功能组件.因为身在H5-Hybird的框架部门,最近团队开始尝试使用R ...

  5. sqlplus常用操作命令2

    常用编辑命令:A[ppend] text 将text附加到当前行之后C[hange] /old /new 将当前行中的old替换为newCLear] buff[er] 清除缓冲区中的所有行DEL 删除 ...

  6. 【中途相遇+二进制】【NEERC 2003】Jurassic Remains

    例题25  侏罗纪(Jurassic Remains, NEERC 2003, LA 2965) 给定n个大写字母组成的字符串.选择尽量多的串,使得每个大写字母都能出现偶数次. [输入格式] 输入包含 ...

  7. Linux Tomcat7.0安装配置实践总结

    一,安装JDk 先下载jdk,链接http://www.oracle.com/technetwork/java/javase/downloads/index.html,选择相对应平台的JDK.由于笔者 ...

  8. dojo.declare

    参考:http://www.ibm.com/developerworks/cn/web/1203_xiejj_dojodeclare/

  9. windows驱动编程(目录)

    目录 第一章 入门 配置开发环境 第一个程序 应用程序调用内核函数的流程

  10. vs2012配置opencv及简单测试

    为visual studio2012搭建openCV平台,实现打开图片. 实现步骤: 1.1.配置环境变量 基于win7操作系统的环境配置步骤: 1.1.1 计算机—>属性—>更改设置—& ...