[ACM] HDU 1242 Rescue (优先队列)

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

 

解题思路:

简单的优先队列题目。复习一下优先队列用法.

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn=202;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int sx,sy,ex,ey;
int n,m;

struct Node
{
    int x,y;
    int step;
};
int step[maxn][maxn];

bool operator<(Node a,Node b)
{
    return a.step>b.step;
}

bool ok(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]!='#')
        return true;
    return false;
}

void BFS(int x,int y)
{
    step[x][y]=0;
    memset(vis,0,sizeof(vis));
    vis[x][y]=1;
    priority_queueq;
    Node tp;
    tp.x=x,tp.y=y,tp.step=0;
    q.push(tp);
    while(!q.empty())
    {
        Node first=q.top();
        if(first.x==ex&&first.y==ey)
            break;
        q.pop();
        for(int i=0;i<4;i++)
        {
            int nextx=first.x+dx[i];
            int nexty=first.y+dy[i];
            if(!vis[nextx][nexty]&&ok(nextx,nexty))
            {
                vis[nextx][nexty]=1;
                tp.x=nextx;
                tp.y=nexty;
                if(mp[nextx][nexty]=='x')
                {
                    step[nextx][nexty]=first.step+2;
                    tp.step=first.step+2;
                }
                else
                {
                    step[nextx][nexty]=first.step+1;
                    tp.step=first.step+1;
                }
                q.push(tp);
            }
        }
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
        {
            cin>>mp[i][j];
            if(mp[i][j]=='r')
                sx=i,sy=j;
            else if(mp[i][j]=='a')
                ex=i,ey=j;
        }
        BFS(sx,sy);
        if(!vis[ex][ey])
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",step[ex][ey]);
    }
    return 0;
}

 

优先队列用法:

///优先队列是默认int从大到小priority_queueq1,也可以定义为从小到大priority_queue,greater >q2;
也可以自定义优先级,重载<

#include 
#include 
#include 
using namespace std;

///自定义优先级,两种写法,按照优先级从大到小的顺序
/*
struct node
{
    friend bool operator<(node n1,node n2)
    {
        return n1.priorityq1;
    for(i=0;i,greater >q2;
    for(i=0;i,greater >q;
    for(int i=0;iq3;
    node b[len];
    b[0].priority=6;b[0].value=1;
    b[1].priority=9;b[1].value=5;
    b[2].priority=2;b[2].value=3;
    b[3].priority=8;b[3].value=2;
    b[4].priority=1;b[4].value=4;
    for(i=0;i


运行:

96532
23569

优先级  值
9       5
8       2
6       1
2       3
1       4


 

你可能感兴趣的:(ACM题目,[ACM]_搜索)