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. App 推荐:Spotify

    之前一直用网易云音乐听歌,后来因为沸沸扬扬的 XCode 注入病毒事件,就把它卸了(无奈脸=.=) 对比国内其它音乐App,私心觉得网易云音乐还是不错的.没有很多花里胡哨的装饰,里面的歌单做的也还算精 ...

  2. poj 2411 Mondriaan's Dream(状态压缩dp)

    Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...

  3. 用Python实现九九乘法表

    1.用“#”组成的矩形的实现 代码 eight = int(input("Height:")) #用户输入高度 width = int(input("Width:&quo ...

  4. Spring的IOC

    引用:http://www.cnblogs.com/xdp-gacl/p/4249939.html 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念 ...

  5. hdu 3954 Level up(线段树)

    题目链接:hdu 3954 Level up 题目大意:N个英雄,M个等级,初始等级为1,给定每一个等级须要的经验值,Q次操作,操作分两种,W l r x:表示l~r之间的英雄每一个人杀了x个怪物:Q ...

  6. 【数学三角恒等变幻】【HDU2552】三足鼎立

    Problem Description MCA山中人才辈出,洞悉外界战火纷纷,山中各路豪杰决定出山拯救百姓于水火,曾以题数扫全场的威士忌,曾经高数九十九的天外来客,曾以一剑铸十年的亦纷菲,歃血为盟,盘 ...

  7. 虚拟化之KVM的安装篇

    1,在安装KVM之前,首先需要在自己的电脑开启cpu虚拟化技术(这个需要BIOS中设置). 如下操作都是在本人虚拟机中设置,所以可以看到我多添加一块磁盘,目的是用来做kvm虚拟机的磁盘. 另外,虚拟机 ...

  8. Java - 网络编程(NetWork)

    Java - 网络编程(NetWork)   一.java.net包下的 InetAddress 类的使用:     > 一个 InetAddress 代表着一个IP地址     > 主要 ...

  9. jQuery中$.getJSON的返回值问题

    在使用$.getJSON获得数据库的返回值后,想将该值return传给其他函数.结果遇到问题. $.getJSON(url, data, function(result) { return resul ...

  10. js 进阶笔记

    JS中substr和substring的用法和区别 substr和substring都是JS截取字符串函数,两者用法很相近, substr方法 返回一个从指定位置开始的指定长度的子字符串. strin ...