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

 

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

菜鸟分析:让r救a么 ,但是r有很多个,你就从a开始找最近的r,测试案例比较特殊,他正好有一个朋友,容易误导,每个朋友有好几种营救方法,要找到离他最近的朋友,再从这些路中找出最短的。

测试案例

这条蓝色的是最短的 蓝色的加上杀警卫的时间+1=13,红色的=14

最短路径问题BFS?

(谢谢大佬)参考题解https://blog.csdn.net/u013923947/article/details/2317421

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int n,m;
int xa,ya,xr,yr;
char map[220][220];
int vis[220][220];
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int x,y,step;
    bool operator < (const node& t)const{
        return step>t.step;
    }
};
int  bfs()
{
    memset(vis,0,sizeof(vis));
    priority_queue<node>q;
    node f;
    f.x=xa;
    f.y=ya;
    f.step=0;
    vis[xa][ya]=1;
    q.push(f);
    while(!q.empty())
    {
        f=q.top();
        q.pop();
        if(map[f.x][f.y]=='r')
        {
          return f.step;
        }
        // 朋友不只是一个,而天使却是一个,所以我们可以反过来寻找,天使找朋友,那么目标状态就是找到朋友
        for(int i=0;i<4;i++)
        {
        node next;
        int xi=f.x+dis[i][0];
        int yi=f.y+dis[i][1];
            if(xi>=0 &&xi<n && yi>=0 && yi<m && map[xi][yi]!='#' && !vis[xi][yi])
            {
                vis[xi][yi]=1;
                next.x=xi;
                next.y=yi;
                if(map[xi][yi]=='x')
                    next.step=f.step+2;
                else
                    next.step=f.step+1;
                q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    while(cin>> n >> m)
    {

      for(int i=0;i<n;i++)
      {
        scanf("%s",map[i]);
        for(int j=0;j<m;j++)
    {
        if(map[i][j]=='a')
        {
            xa=i;
            ya=j;
        }
    }
    }
   int ans=bfs();
    if(ans==-1)
        printf("Poor ANGEL has to stay in the prison all his life.\n");
    else
        printf("%d\n",ans);
  }

    return 0 ;
}

以下是错误代码只作参考

#include<iostream> 
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int n,m;
char map[205][205];
int vis[205][205];
/*两个作用
1.标记作用,刚开始初始化为1,先将起始点初始化为0,以防回到起点 
2.记录当前点的最短路径 
*/
int x1,x2,y1,y2;
int mov[][2] = {1,0,-1,0,0,1,0,-1};

struct node
{
    int x,y,step;
    //友元函数 重载<
    //优先队列就是node型的  在优先队列里按照step排序
    friend bool operator<(node n1,node n2)
    {
        return n2.step<n1.step;
    }
};

int place(int x,int y)
{
    if(x<0 || y<0 || x>=n || y>=m || !vis[x][y] || map[x][y] == '#')
        return 1;//出界或者为墙
    return 0;
}

int bfs()
{
    int i;
    priority_queue<node> Q;//优先队列
    node a,next;
    a.x = x1;
    a.y = y1;
    a.step = 0;
    Q.push(a);//进队
    vis[x1][y1] = 0;//初始化
    while(!Q.empty())
    {
        a = Q.top();//队头
        Q.pop();//出队
        if(a.x == x2 && a.y == y2)
            return a.step;
            //四个方向
        for(i = 0; i<4; i++)
        {
            next = a;//移动后的结点
            next.x+=mov[i][0];
            next.y+=mov[i][1];
            if(place(next.x,next.y))//判断 走不通
                continue;//跳出当前循环
            next.step++;//每动一次要加1
            if(map[next.x][next.y] == 'x')//卫兵处多花费了一秒
                next.step++;
            if(vis[next.x][next.y]>=next.step)//存入最小时间
            {
                vis[next.x][next.y] = next.step;//第二个作用 更新最小 
                Q.push(next);
            }//只有起点标记,其它点未标记只是最小值,还可以走 
        }
    }
    return 0;
}

int main()
{
    while(cin>>n>>m)
    {
        for(int i = 0; i<n; i++)
        {
            scanf("%s",map[i]);
            for(int j = 0; map[i][j]; j++)
            {
                if(map[i][j] == 'r')
                {
                    x1 = i;
                    y1 = j;
                }
                else if(map[i][j] == 'a')
                {
                    x2 = i;
                    y2 = j;
                }
            }
        }
        memset(vis,1,sizeof(vis));
        int ans = 0;
        ans = bfs();
        if(ans)
            printf("%d\n",ans);
        else
            printf("Poor ANGEL has to stay in the prison all his life.\n");
    }

    return 0;
}//Time Limit Exceeded

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值