G - Rescue 【地图型BFS+优先队列(有障碍物)】

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.) 

InputFirst 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. 
OutputFor 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'是警卫,走路花费一秒,遇见警卫花费两秒。
问最小多少秒能救人,
能的话输出步数,不能的话输出"Poor ANGEL has to stay in the prison all his life."*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define inf 0x3fffffff
using namespace std;

const int maxn = 205;

int n,m,xxx,yyy;
char a[maxn][maxn];
int vis[maxn][maxn];
int dir[][2]={ {1,0},{0,1},{-1,0},{0,-1} };


struct Node
{
    int xx,yy,time;  //横纵坐标+时间
   friend bool operator < (Node a,Node b)
    {
        return a.time > b.time;
    }
};

int check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m||a[x][y]=='#'||vis[x][y])
        return 0;
    return 1;
}
/*int check(int x,int y)
{
    if(x<0 || y<0 || x>=n || y>=m || !vis[x][y] || a[x][y] == '#')
        return 1;
    return 0;
}*/

int bfs(int x,int y)
{
    memset(vis,0,sizeof(vis));//清空标记数组放到bfs里面!!!!!
    priority_queue<Node> q;
    while(!q.empty()) q.pop();
    q.push(Node{x,y,0});
    vis[x][y]=1;

    while(!q.empty())
    {
        Node u=q.top();
        q.pop();
        Node tmp;
        if(a[u.xx][u.yy]=='r')
            return u.time;
        for(int i=0;i<4;i++)
        {
            tmp.xx=u.xx+dir[i][0];
            tmp.yy=u.yy+dir[i][1];

            if( check(tmp.xx,tmp.yy) )
            {
                vis[tmp.xx][tmp.yy]=1;
                if(a[tmp.xx][tmp.yy]=='x')
                tmp.time=u.time+2;
                else
                tmp.time=u.time+1;
                q.push(tmp);
            }
        }
    }
    return -1;
}


int main()
{
    memset(vis,0,sizeof(vis));
    while(~scanf("%d%d",&n,&m))
{
    for(int i=0;i<n;i++)
    {
        scanf("%s",&a[i]);
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='a')
            {
                xxx=i;
                yyy=j;
                break;
            }
        }
    }

    int ans=bfs(xxx,yyy);
    if(ans==-1)
         printf("Poor ANGEL has to stay in the prison all his life.\n");
    else
         printf("%d\n",ans);
}

}
View Code

 

转载于:https://www.cnblogs.com/Roni-i/p/7407102.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值