Rescue

来源:互联网 发布:淘宝评价无法上传照片 编辑:程序博客网 时间:2024/06/03 11:48
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

题意:天使(a)被困于迷宫,它的朋友(r)去救她,在迷宫中会有守卫(x)。r每走一步耗费一个单位的时间,
如果路途遇上x,杀死x则需要一个单位的时间,求r找到a的最短时间如果找不到就输出"Poor ANGEL has to stay in the prison all his life."

题目说有多个朋友(起点),但测试数据只有一个朋友(起点)!

#include <cstdio>#include <cstring>#include <queue>using namespace std;const int inf=9999;struct A{    int x,y,step;    friend bool operator<(A x,A y)    {        return x.step>y.step;//步数小的小厨队列    }}f,g;int n,m,vis[201][201];int value[201][201];char map1[201][201];int sx,ex,sy,ey;int dx[]={-1,0,1,0};int dy[]={0,1,0,-1};int bfs(int x,int y){    priority_queue<A> Q;    f.x=x;    f.y=y;    f.step=0;    vis[x][y]=1;    Q.push(f);    while(!Q.empty())    {        f=Q.top();        Q.pop();        if(f.x==ex&&f.y==ey)            return f.step;        for(int i=0;i<4;i++)        {            g.x=f.x+dx[i];            g.y=f.y+dy[i];            if(!vis[g.x][g.y]&&g.x>=0&&g.y>=0&&g.x<n&&g.y<m&&map1[g.x][g.y]!='#')            {                vis[g.x][g.y]=1;                if(map1[g.x][g.y]=='x')                    g.step=f.step+2;                else                    g.step=f.step+1;                value[g.x][g.y]=g.step;                Q.push(g);            }        }    }    return 0;}int main(){    int i,j,ans;    while(~scanf("%d %d",&n,&m))    {        memset(value,inf,sizeof(vis));        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++)        {            getchar();            for(int j=0;j<m;j++)            {                scanf("%c",&map1[i][j]);                if(map1[i][j]=='a')                {                    ex=i;                    ey=j;                }                if(map1[i][j]=='r')                {                    sx=i;                    sy=j;                }            }        }        ans=0;        ans=bfs(sx,sy);        if(ans)            printf("%d\n",ans);        else            printf("Poor ANGEL has to stay in the prison all his life.\n");    }    return 0;}/*7 8axxxxxxr........#..#x.....#..#.##...##...#..............7 8ax..xxxr..##xx....##......####....##......##............*/


原创粉丝点击