现在的位置: 首页 > 综合 > 正文

HDU 1242 Rescue BFS

2018年01月19日 ⁄ 综合 ⁄ 共 2489字 ⁄ 字号 评论关闭

Rescue

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

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
/*
BFS 宽度优先搜索 
X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙
路花费一秒,x花费两秒
可能有多个'r’,而'a’只有一个,从’a’开始搜,找到的第一个’r’即为所求
BFS+优先队列
普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。
在优先队列中,元素被赋予优先级。
当访问元素时,具有最高优先级的元素最先删除。
优先队列具有最高进先出 (largest-in,first-out)的行为特征。
*/

#include<iostream>
#include<stdio.h>
#include<queue>

using namespace std;

struct node
{
    int x,y,step;
    friend bool operator < (const node &a,const node &b) //时间小的先出列
    {
        return a.step>b.step;
    } 
    
};

char map[201][201];
int visit[201][201];
int direc[4][2]={-1,0,1,0,0,1,0,-1};
int  n,m;

int check(int x,int y)
{
    if (x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#') //符合条件的 
        return 1;
    return 0;
}
 
 
int BFS(int x,int y)
{
    int i;
    node start,end;
    priority_queue<node> que;
    memset(visit,0,sizeof(visit));
    
    start.x=x;
    start.y=y;
    start.step=0;
    
    visit[start.x][start.y]=1;
    que.push(start);
    
    while(!que.empty())
    {
        start=que.top();        //取队首 出队首 
        que.pop();    
        if(map[start.x][start.y]=='r')
            return start.step;
        for(i=0;i<4;i++)        //标记相邻节点 
        {
            end.x=start.x+direc[i][0];
            end.y=start.y+direc[i][1];
            if(check(end.x,end.y)&&!visit[end.x][end.y])
            {
                visit[end.x][end.y]=1;
                if(map[end.x][end.y]=='x')
                    end.step=start.step+2;
                else
                    end.step=start.step+1;
                que.push(end);            //入队尾 
            }
            
            
        }
    }
    return -1;
    
}
int main(){
    
    int x,y,i,j,ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(j=0;j<m;j++)
                if(map[i][j]=='a')
                    {
                        x=i;y=j;
                    }
        }
        ans=BFS(x,y);
        if(ans==-1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");  
        else  
            printf("%d\n",ans); 
    }
    return 0;
}

抱歉!评论已关闭.