【hdoj 1242】Rescue

来源:互联网 发布:绝地求生有数据接口吗 编辑:程序博客网 时间:2024/06/02 02:15
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19969 Accepted Submission(s): 7106


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


题意理解:天使被关在监狱里,天使的朋友要去救他(不止一个朋友),当遇上警卫时可以杀掉耗时为1,每走一步的时间也是1,求出天使的盆友救出天使(从朋友的坐标到                       天使的坐标)的最短时间;如果不能到达,就输出"Poor ANGEL has to stay in the prison all his life."

解题思路:天使的朋友不止一个,可以从天使出发,找到天使的朋友,优先队列保存数组,广搜找出最短时间。最开始的时候我是一个一个字符输入的,结果总是不对,用一行                    字符串读入可以省掉许多不必要的麻烦.

 code:

#include <iostream>#include <algorithm>#include <cstdio>#include <string.h>#include <queue>using namespace std;int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};int N,M;struct node{    int x,y,step;};char a[205][205];int v[205][205];queue<node> Q;int sx,sy,ex,ey;int exceed(int a,int b){    if(a<0 || a>=N|| b<0 || b>=M)        return 1;    return 0;}void bfs(){    node p,q;    p.x=sx;    p.y=sy;    p.step=0;    Q.push(p);    while(!Q.empty())    {        p=Q.front();        Q.pop();        if(p.x==ex&&p.y==ey)   //遇到朋友就输出时间,退出        {            printf("%d\n",p.step);            return;        }        for(int i=0;i<4;i++)        {            q.x=p.x+dir[i][0];            q.y=p.y+dir[i][1];            if(exceed(q.x,q.y))  //超出边界                continue;            if(a[q.x][q.y]=='#')   //墙,不能走                continue;            if(v[q.x][q.y])       //已经访问过该点                continue;            if(a[q.x][q.y]=='.'||a[q.x][q.y]=='r')               {                q.step=p.step+1;                v[q.x][q.y]=1;                Q.push(q);            }            if(a[q.x][q.y]=='x')    //遇上警卫            {                q.step=p.step+2;      //走过的时间加消灭警卫的时间                v[q.x][q.y]=1;                Q.push(q);            }        }    }    printf("Poor ANGEL has to stay in the prison all his life.\n");}int main(){    while(scanf("%d%d",&N,&M)!=EOF)    {        while(!Q.empty())            Q.pop();        for(int i=0;i<N;i++)        {            scanf("%s",a[i]);            for(int j=0;j<M;j++)            {                 if(a[i][j]=='a')                 {                     sx=i;                     sy=j;                 }                 if(a[i][j]=='r')                 {                     ex=i;                     ey=j;                 }            }        }        memset(v,0,sizeof(v));        v[sx][sy]=1;        bfs();    }    return 0;}





0 0
原创粉丝点击