Rescue HDU杭电1242【广搜+优先队列】

9 篇文章 0 订阅
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
 


#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
char map[220][220];
bool vis[220][220];
int m,n;
int ex,ey;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
struct rescue
{
    int x,y;
    int time;
    friend bool operator < (rescue a,rescue b)
    {
        return a.time>b.time;//小的先出队列 
    }
};

bool judge(rescue a)
{
    if(a.x>m||a.x<1||a.y>n||a.y<1||map[a.x][a.y]=='#'||vis[a.x][a.y]) return 0;

    return 1;
}
int BFS(int sx,int sy)
{
    rescue pos,next;
    priority_queue<rescue>q;
    pos.x=sx;
    pos.y=sy;
    pos.time=0;
    vis[sx][sy]=1;
    q.push(pos);
    while(!q.empty())
    {
        pos=q.top();
        q.pop();
        for(int i=0;i<4;++i)
        {
            next.x=pos.x+dx[i];
            next.y=pos.y+dy[i];                                                    
            if(judge(next))
            {
                if(map[next.x][next.y]=='x') next.time = pos.time+2;
                else next.time = pos.time+1;
                if(next.x==ex && next.y==ey)
                {
                    return next.time;
                     
                }                
                vis[next.x][next.y]=1;
                q.push(next);
            }                                                                        
        }
    }    
    return -1;
}
int main()
{

    int x,y;
    while(~scanf("%d%d",&m,&n))
    {
        //int num=0;
        getchar();
        for(int i=1;i<=m;++i)
        {
            for(int j=1;j<=n;++j)
            {
                scanf("%c",map[i]+j);
                if(map[i][j]=='r')
                {
                    x=i,y=j;
                }
                else if(map[i][j]=='a')
                {
                    ex=i;ey=j;
                }
                //else if(map[i][j]=='x') ++num;
            }
            getchar();
        }
        memset(vis,0,sizeof(vis));
        int min_time;
        min_time=BFS(x,y);
        if(min_time==-1)
        	printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
        	printf("%d\n",min_time);
    }
    return 0;
}


#include<cstdio>
#include<queue>
#include<algorithm>
#define INF 0xfffffff
using namespace std;
char map[220][220];
int m,n;
int ex,ey;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
int min_time;
struct rescue
{
    int x,y;
    int time;
    friend bool operator < (rescue a,rescue b)
    {
        return a.time>b.time;//小的先出队列 
    }
};

bool judge(rescue a)
{
    if(a.x>m||a.x<1||a.y>n||a.y<1||map[a.x][a.y]=='#') return 0;
//    if(a.time>=min_time) return 0;
    return 1;
}
int BFS(int sx,int sy)
{
    rescue pos,next;
    priority_queue<rescue>q;
    pos.x=sx;
    pos.y=sy;
    pos.time=0;
    map[sx][sy]='#';
    q.push(pos);
    while(!q.empty())
    {
        pos=q.top();
        q.pop();
        for(int i=0;i<4;++i)
        {
            next.x=pos.x+dx[i];
            next.y=pos.y+dy[i];                                        
            if(map[next.x][next.y]=='x') next.time = pos.time+2;
            else next.time = pos.time+1;
            
            if(judge(next))
            {
                if(next.x==ex && next.y==ey)
                {
                    min_time=next.time;
                    return min_time;
                }
                map[next.x][next.y]='#';
                q.push(next);
            }                                                                        
        }
    }
	return -1;    
}
int main()
{

    int x,y;
    while(~scanf("%d%d",&m,&n))
    {
        //int num=0;
        getchar();
        for(int i=1;i<=m;++i)
        {
            for(int j=1;j<=n;++j)
            {
                scanf("%c",map[i]+j);
                if(map[i][j]=='r')
                {
                    x=i,y=j;
                }
                else if(map[i][j]=='a')
                {
                    ex=i;ey=j;
                }
                //else if(map[i][j]=='x') ++num;
            }
            getchar();
        }
        min_time = BFS(x,y);
        if(min_time==-1)
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		else
        	printf("%d\n",min_time);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值