HDU 1242Rescue(bfs+优先队列)

来源:互联网 发布:知乎 漫威 彩蛋 编辑:程序博客网 时间:2024/06/05 03:54

Rescue

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

Source

ZOJ Monthly, October 2003
想法:bfs+优先队列
代码:
#include <stdio.h>#include <string.h>#include <queue>using namespace std;struct node{    int x,y,step;    friend bool operator < (node n1,node n2)    //自定义优先级。在优先队列中,优先级高的元素先出队列。    {         return n1.step > n2.step;    //通过题意可知 step 小的优先级高,需要先出队。    }};char map[205][205];int vis[205][205];int to[4][2]= {1,0,-1,0,0,1,0,-1};int n,m,sx,sy,ex,ey,ans;int check(int x,int y){    if(x<0 || x>=n || y<0 || y>=m)        return 1;    if(vis[x][y] || map[x][y]=='#')        return 1;    return 0;}void bfs(){    int i;    priority_queue<node> Q;    node a,next;    a.x = sx;    a.y = sy;    a.step = 1;    vis[a.x][a.y]=1;    Q.push(a);    while(!Q.empty())    {        a = Q.top();        Q.pop();        if(map[a.x][a.y]=='a')        {            ans = a.step;            return ;        }        for(i = 0; i<4; i++)        {            next = a;            next.x+=to[i][0];            next.y+=to[i][1];            if(check(next.x,next.y))                continue;            if(map[next.x][next.y]=='.') next.step=a.step+1;            if(map[next.x][next.y]=='x') next.step=a.step+2;            vis[next.x][next.y] = 1;            Q.push(next);        }    }    ans = -1;}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        int i,j;        for(i = 0; i<n; i++)            scanf("%s",map[i]);        for(i = 0; i<n; i++)        {            for(j = 0; j<m; j++)            {                if(map[i][j]=='r')                {                    sx = i;                    sy = j;                }                else if(map[i][j]=='a')                {                    ex = i;                    ey = j;                }            }        }        memset(vis,0,sizeof(vis));        ans=0;        bfs();        if(ans!=-1)         printf("%d\n",ans);        else         printf("Poor ANGEL has to stay in the prison all his life.\n");    }    return 0;}


原创粉丝点击