hdu1242 bfs

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24608    Accepted Submission(s): 8686

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

 


'x'进队两次,第一次进队标记为'o','o'出队的时候标记为'.',然后'.'再次进队,之后的处理和其他点一样

因为要保证bfs同层的t相同,这样a第一次找到'r'就能保证时间最短

好像'r'好像可以有多个,所以本题最好'a'去找'r'

以前在zoj做过这题,还是缠了好久。。。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>

const int inf =(1<<31)-1;
const int MAXN = 2e2+10;

using namespace std;

struct step{
    int x;
    int y;
    int t;
};

int mov[4][2]={1,0,-1,0,0,1,0,-1};
queue<step>q;
char G[MAXN][MAXN];
int n,m;

int check(int x,int y){
    if(x<0||y<0||x>=n||y>=m)return 0;
    if(G[x][y]=='#')return 0;
    else return 1;
}

int main()
{
    int sx,sy;
    int mmint;
    while(~scanf("%d%d",&n,&m)){
        mmint = -1;
        for(int i=0;i<n;i++){
            scanf("%s",G[i]);
            for(int j=0;j<m;j++){
                if(G[i][j]=='a'){
                    sx = i;
                    sy = j;
                }
            }
        }
        step t,fro;
        t.x=sx;
        t.y = sy;
        t.t = 0;
        q.push(t);
        G[sx][sy]='.';
        int nx,ny;
        while(!q.empty()){
            fro = q.front();
            q.pop();
            if(G[fro.x][fro.y]=='r'){
                mmint = fro.t;
                break;
            }
            if(G[fro.x][fro.y]=='o'){
                G[fro.x][fro.y] = '.';
                fro.t = fro.t+1;
                q.push(fro);
               // cout<<"debug"<<endl;
                continue;
            }
            for(int i=0;i<4;i++){
                nx = fro.x+mov[i][0];
                ny= fro.y+mov[i][1];
                if(check(nx,ny)){
                    if(G[nx][ny]=='x'){
                        G[nx][ny]='o';
                        //cout<<"start"<<endl;
                    }else if(G[nx][ny]=='o'){
                        continue;
                    }else if(G[nx][ny]!='r'){
                        G[nx][ny]='#';
                    }
                    //G[nx][ny]='#';
                    t.x = nx;
                    t.y = ny;
                    t.t = fro.t+1;
                    q.push(t);
                }
            }
        }
        while(!q.empty()){
            q.pop();
        }
        if(mmint==-1){
            cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
        }else{
            cout<<mmint<<endl;
        }
    }
    //cout << "Hello world!" << endl;
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/EdsonLin/p/5434764.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值