Rescue

Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1407 Accepted Submission(s): 548
 
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
题目大意:给出地图,天使的位置用a表示(终点),r表示天使的朋友(起点,不一定只有一个),x是守卫,.是路,#是墙
可以往四个方向走,每走一步耗费1个体力,碰见守卫再+1,问你天使见到朋友需要多长时间。
bfs 吧终点起点倒过来,这样起点唯一,相当于找最近的一个r。
#include<queue>
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define N 1234
int n, m, a, b, x, y, cnt, ans;
struct node
{
    int x, y, time;
    bool operator < (const node & t)const
    {
        return time > t.time;   
    }
}st;
priority_queue<node>q; 
char mat[N][N];
int v[N][N];
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
int bfs()
{
    memset(v ,0 , sizeof(v));
    while(!q.empty()) q.pop();
    q.push(st);
    v[st.x][st.y] = true;
    while(!q.empty())
    {
        st = q.top();  
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            node t = st;
            t.x += dx[i], t.y +=dy[i];
            if(v[t.x][t.y] || mat[t.x][t.y]=='#')
                     continue;
            if(t.x<1 || t.y<1 || t.x>n || t.y>m)
                     continue;
            if(mat[t.x][t.y] == '.')t.time++;
            if(mat[t.x][t.y] == 'x')t.time+=2;
            if(mat[t.x][t.y] == 'r') return t.time+1;
            q.push(t);
            v[t.x][t.y] = true;
            }
    }
    return -1;
}
int main()
{
    while(cin>>n>>m)
    {
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            scanf(" %c", &mat[i][j]);
            if(mat[i][j] == 'a')
                st.x = i, st.y = j, st.time = 0;
        }
        ans = bfs();
        if(ans == -1)puts("Poor ANGEL has to stay in the prison all his life.");
        else cout<<ans<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值