HDU1242 Rescue(BFS松弛)

Rescue
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 1242
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

这题没什么别的话说,自己蠢=-=
如果一直往队列里push 就会爆内存。
BFS:如果当前的结果优于原先的值则更新原先值。然后再判断有没有入队。如果已经入队了就不用再拉入了。当这个节点作为父亲节点使用完后,记得要释放掉,因为他之后可能会被更新然后再次入队去更新他的儿子们。
长这么大第一次觉得写搜索好累。。。大概是窝已经忘了以前写搜索的感觉了吧qwq

//  Created by ZYD in 2015.
//  Copyright (c) 2015 ZYD. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define Size 100000
#define ll long long
#define mk make_pair
#define pb push_back
// #define mem(array) memset(array,0,sizeof(array))
// typedef pair<int,int> P;
int xx[4]={-1,0,0,1};
int yy[4]={0,-1,1,0};

struct sta{
    int x;
    int y;
};
queue<sta> q;

char mp[205][205];
int tim[205][205],in[205][205];
int ans,flag,n,m;
bool can(int x,int y){
    if(1<=x && x<=n && 1<=y && y<=m && mp[x][y]!='#' ) return true;
    return false;
}
int bfs(){
    struct sta u;
    struct sta v;
    while(q.size()){
        u=q.front();q.pop();
        if(mp[u.x][u.y]=='a') {flag=1;ans=tim[u.x][u.y];return 0;}
        for(int i=0;i<4;i++){
            sta v;
            v.x=u.x+xx[i];
            v.y=u.y+yy[i];
            if(can(v.x,v.y) && (tim[v.x][v.y]>tim[u.x][u.y]+1) ){
                tim[v.x][v.y]=tim[u.x][u.y]+1;
                if(mp[v.x][v.y]=='x') tim[v.x][v.y]++;
                if(in[v.x][v.y]==0)  
                    {
                        q.push(v);
                        in[v.x][v.y]=1;
                    }
            }
        }
        in[u.x][u.y]=0;
    }       
    return 0;   
}

int main()
{
    freopen("in.txt","r",stdin);
    while(~scanf("%d %d\n",&n,&m)){
        memset(tim,0x3f3f3f,sizeof tim);
        memset(mp,0,sizeof mp);
        memset(in,0,sizeof in);
        flag=0;//cout<<in[1][1]<<endl;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%c",&mp[i][j]);
                // cout<<mp[i][j];
                if(mp[i][j]=='r'){
                    sta p;
                    p.x=i;p.y=j;
                    q.push(p);  //cout<<q.size()<<"*"<<endl;
                    tim[i][j]=0;
                    in[i][j]=1;
                }
            }
            getchar();
        }
        bfs();
        if(flag==1)printf("%d\n",ans);
        else printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值