Rescue(bfs加点dp的思想优化)

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,网上还有其他方法(优先队列和dfs,bfs),但是感觉还是我的bfs最快了

提交4次基本都是10ms

在bfs拓展入队的时候加入一点dp的思想,当前这个点被第二访问时,选择最短的步数即可

还有就是用pair封装坐标可能好看点吧。。。QAQ

典型的BFS模板题

#include <cstdio>
#include <queue>
#include <utility>
using namespace std;

const int N=201;
typedef pair <int,int > p;//用pari封装一下坐标
typedef queue <p> q;
int m,n;
char s[N][N];
int dir[4][2]={0,1,1,0,-1,0,0,-1};
int num[N][N];
int a,b;
int e1,e2;//起点
int ans;
int f1,f2;//终点

void bfs()
{
    int step=0,x,y;
    q qu;
    p pa;
    pa = make_pair(a,b);
    qu.push(pa);
    while(!qu.empty())
    {
        pa = qu.front();
         qu.pop();
        x=pa.first,y=pa.second;
        if( x== e1 && y== e2)
        {
            //printf("num=%d\n",num[x][y]);
            if(ans>num[x][y]) ans=num[x][y];
            //return;
        }
        for(int i=0;i<4;i++)
        {
            int xx,yy;
            xx=x+dir[i][0];
            yy=y+dir[i][1];
            if(xx>=0 && yy>=0 &&xx<n && yy<m && s[xx][yy]!='#' &&(!num[xx][yy] || num[x][y]+1<num[xx][yy]))//xx,yy这个点没有走过 ||  xx,yy这个点走过,但是不是最优
            {
                pa = make_pair(xx,yy);
                qu.push(pa);
                num[xx][yy]=num[x][y]+1;
                if(s[xx][yy]=='x') num[xx][yy]++;
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        ans=10000;
        for(int i=0; i<n; i++)
            scanf("%s",s[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                if(s[i][j]=='a')
                    e1=i,e2=j;//记录起点
                if(s[i][j]=='r')
                    a=i,b=j;//记录终点
                num[i][j]=0;
            }
        num[a][b]=0;
        bfs();
        if(ans==10000) printf("Poor ANGEL has to stay in the prison all his life.\n");
        else printf("%d\n",ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值