HDU - 1242 Rescue

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36265    Accepted Submission(s): 12541


 

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

 题意:angel被困了,他的朋友们去救她,他的朋友不止一个,最近能救到她要花多少步,“#”不能走,“.”可以走,当碰到“x”时要杀死它所以花费两步。

思路我自己写的太麻烦了看了下别人的博客,bfs用优先队列,因为优先队列每次出来的就是最小的,因为朋友不止一个,那我们先找到angel,让她走去找朋友,找到离她最近的那一个朋友就算成功。巧妙地用优先队列解决问题

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m;
char mapp[205][205];
int vis[205][205];
struct node
{
    int x,y,step;
    friend bool operator <(node a,node b)
    {
        return a.step>b.step;
    }
};
int dx[4]= {0,1,0,-1},dy[4]= {1,0,-1,0};
int xx,yy;
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&mapp[x][y]!='#')
        return 1;
    return 0;
}
int bfs()
{
    priority_queue<node>q;
    while(!q.empty()) q.pop();
    memset(vis,0,sizeof(vis));
    vis[xx][yy]=1;
    node st,ed;
    st.x=xx;
    st.y=yy;
    st.step=0;
    q.push(st);
    while(!q.empty())
    {
        st=q.top();
        q.pop();
        if(mapp[st.x][st.y]=='r')
            return st.step;
        for(int i=0; i<4; i++)
        {
            ed.x=st.x+dx[i];
            ed.y=st.y+dy[i];
            if(judge(ed.x,ed.y))
            {
                vis[ed.x][ed.y]=1;
                if(mapp[ed.x][ed.y]=='x')
                    ed.step=st.step+2;
                else
                    ed.step=st.step+1;
                q.push(ed);
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0; i<n; i++)
        {
            scanf("%s",mapp[i]);
            for(int j=0; j<m; j++)
            {
                if(mapp[i][j]=='a')
                {
                    xx=i;
                    yy=j;
                }
            }
        }
        int ans=bfs();
        if(ans==-1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
        {
            printf("%d\n",ans);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/zezzezzez/article/details/81142208