• 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
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    父组件向子组件传递数据(vue.js)
    vue引入JQ的方法
    webstorm添加*.vue文件代码提醒支持webstorm支持es6vue里支持es6写法
    创建脚手架步骤
    JS严格校验身份证号
    微信小程序开发工具 常用快捷键
    GIT 常用命令
    git 操作
    通过selenium(也有Puppeter版在最后)登录网页获取特定信息
    用Django ORM实现树状结构
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5434764.html
Copyright © 2020-2023  润新知