Rescue

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
 


<p> 1、采用优先队列(按到达该点的时候),这样就能保证最先扩展到目标状态的时候,耗时是最小的。</p><p> 2、把当扩展到“x”点时,把他放到与他耗时一样的那一层。这样也能保证到扩展到目标状态时,耗时是最小的。</p>

#include <iostream>  
#include <cstring>  
#include <cstdio>  
#include <queue>  
using namespace std; 
int n, m; 
char map[205][205]; 
int sx, sy; 
bool flag; 
struct node { 
    int x, y, step; 
    bool operator <(const node & t) const 
    { 
        return step>t.step; 
    } 
}; 
int dx[]= {-1,0,0,1}; 
int dy[]= {0,-1,1,0}; 
 
void bfs() { 
    node now, tmp; 
    int i,xx,yy; 
    priority_queue<node> q; 
    now.x = sx, now.y = sy, now.step = 0; 
    map[sx][sy] = '#'; 
    q.push(now); 
    while(!q.empty()) { 
        now = q.top(); 
        q.pop(); 
//         cout<<now.x<<" "<<now.y<<" "<<now.step<<endl;  
        for(i=0; i<4; i++) { 
            xx = now.x +dx[i]; 
            yy = now.y +dy[i]; 
            if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]=='#') continue; 
            if(map[xx][yy]=='r') { 
                cout<<now.step+1<<endl; 
                flag = true; 
                return ; 
            } 
            if(map[xx][yy]=='x') { 
                tmp.x =xx, tmp.y = yy, tmp.step = now.step+2; 
                q.push(tmp); 
            } else { 
                tmp.x =xx, tmp.y = yy, tmp.step = now.step+1; 
                q.push(tmp); 
            } 
            map[xx][yy] = '#'; 
        } 
    } 
} 
 
int main() { 
    int i, j; 
    while(~scanf("%d%d",&n,&m)) { 
        for(i=0; i<n; i++) 
            for(j=0; j<m; j++) { 
                cin>>map[i][j]; 
                if(map[i][j]=='a') 
                    sx=i,sy=j; 
            } 
        flag = false; 
        bfs(); 
        if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n"); 
    } 
    return 0; 
} 



#include <iostream>  
#include <queue>  
#include <cstring>  
using namespace std; 
struct node 
{ 
    int x, y; 
    int step; 
}; 
queue<node> q; 
int N, M, prove, sx, sy, visit[202][202]; 
char map[202][202]; 
int dx[4] = {-1, 1, 0, 0}; 
int dy[4] = {0, 0, 1, -1,}; 
int check(int x, int y) 
{ 
    if(x < 0 || x >= N || y < 0 || y >= M) 
        return 0; 
    else 
        return 1; 
} 
void BFS() 
{ 
    while(!q.empty()) 
        q.pop(); 
    node s, e; 
    memset(visit, 0, sizeof(visit)); 
    s.step = 0;  s.x = sx;  s.y = sy; 
    q.push(s); 
    visit[s.x][s.y] = 1; 
    while(!q.empty()) 
    { 
        s = q.front(); 
        q.pop(); 
        if(map[s.x][s.y] == 'a') 
        { 
            cout << s.step << endl; 
            prove =1; 
        } 
   //如取出的是在有警卫的格子中,即杀掉警卫再次进入队列  
        if(map[s.x][s.y] == 'x')  
        { 
            map[s.x][s.y] = '.'; 
            s.step += 1; 
            q.push(s); 
            continue; 
        } 
        for(int i = 0; i < 4; i++) 
        { 
            e.x = s.x + dx[i];  e.y = s.y + dy[i]; 
            if(!check(e.x, e.y) || visit[e.x][e.y]  
            || map[e.x][e.y] == '#') 
                continue; 
    e.step = s.step + 1; 
            q.push(e); 
            visit[e.x][e.y] = 1; 
 
   } 
    } 
} 
int main() 
{ 
    while(cin >> N >> M) 
    { 
        for(int i = 0; i < N; i++) 
        for(int j = 0; j < M; j++) 
        {     
            cin >> map[i][j]; 
            if(map[i][j] == 'r') 
            { sx = i;  sy = j; } 
        } 
        prove = 0; 
        BFS(); 
        if(!prove) 
            cout << "Poor ANGEL has to stay in the prison all his life." << endl; 
    } 
    return 0; 
} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值