HDU 1242 Rescue(求最短时间救出同伴,BFS+DP)

Rescue

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

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 

题目大意:

你的朋友被抓进监狱,现在你要找到你的朋友,在途中你会遇到墙(’#‘),不能走,路(‘.’)可以走,花时为1秒,士兵(‘X’),花时间为2秒,求最短时间。

解题思路:

BFS和DP,不能单纯的只用BFS,因为有的一步是要花时间2,有的为1,不能确保当前的就是最优的,所有有要用到DP。

代码;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;

struct node{
    int i,j;
    node(int i0=0,int j0=0){
        i=i0,j=j0;
    }
};

const int dirJ[4]={0,1,0,-1};
const int dirI[4]={1,0,-1,0},maxN=220;
int n,m,is,js,ie,je,time[maxN][maxN];
char str[maxN][maxN];

void bfs(){
    queue <node> path;
    time[is][js]=0;
    path.push(node(is,js));
    while(!path.empty()){
        node s=path.front();
        path.pop();
        for(int k=0;k<4;k++){
            int di=s.i+dirI[k],dj=s.j+dirJ[k];
            if(di>=n||dj>=m||di<0||dj<0) continue;
            if(str[di][dj]=='#') continue;
            int tmp=time[s.i][s.j]+1;
            if(str[di][dj]=='x') tmp++;
            if( tmp<time[di][dj] || time[di][dj]==-1 ){//带点dp优化.
                path.push(node(di,dj));
                time[di][dj]=tmp;
            }
        }
    }
    if(time[ie][je]==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
    else printf("%d\n",time[ie][je]);
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(time,-1,sizeof(time));
        for(int i=0;i<n;i++){
            scanf("%s",str[i]);//建议这样。
            for(int j=0;j<m;j++){
                if(str[i][j]=='r'){is=i,js=j;}
                if(str[i][j]=='a'){ie=i,je=j;}
            }
        }
        bfs();
    }
    return 0;
}
时间: 2024-10-01 22:01:06

HDU 1242 Rescue(求最短时间救出同伴,BFS+DP)的相关文章

HDU 1242 Rescue营救 BFS算法

题目链接:HDU 1242 Rescue营救 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16524    Accepted Submission(s): 5997 Problem Description Angel was caught by the MOLIGPY! He was put in prison by

hdu 1242:Rescue(BFS广搜 + 优先队列)

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

ZOJ 1649 &amp;&amp; HDU 1242 Rescue

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

HDU 1242——Rescue(优先队列)

题意: 一个天使a被关在迷宫里,她的许多小伙伴r打算去救她,求小伙伴就到她需要的最小时间.在迷宫里有守卫,打败守卫需要一个单位时间,如果碰到守卫必须要杀死他 思路: 天使只有一个,她的小伙伴有很多,所以可以让天使找她的小伙伴,一旦找到小伙伴就renturn.时间小的优先级高.优先队列搞定 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<

HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 开始以为是水题,想敲一下练手的,后来发现并不是一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚开始并没有觉得像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们竟然用BFS+

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue 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:

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

HDU 1242 -Rescue (双向BFS)&amp;amp;&amp;amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚開始并没有认为像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们居然用BFS+优

hdu 1242 找到朋友最短的时间 bfs+优先队列

找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.............. Sample Output13 bfs+优先队列 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 using names