zoukankan      html  css  js  c++  java
  • Rescue_BFS

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


    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
    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    #include<queue>
    using namespace std;
    const int N=205;
    char mp[N][N];
    int n,m;
    int x1,x2,y1,y2;
    int vis[N][N];//用vis既能记录是否到达过,又记录当前最短时间到达该处
    int di[][2]= {1,0,0,1,-1,0,0,-1};
    struct node
    {
        int x,y;
        int t;
    };
    bool go(int x,int y)
    {
        if(x<=0||x>n||y<=0||y>m||mp[x][y]=='#') return false ;
        else return true;
    }
    void bfs(int x,int y)
    {
        queue<node>qu;
        memset(vis,0,sizeof(vis));
        node cur,next;
        cur.x=x,cur.y=y,cur.t=0;
        qu.push(cur);
        while(!qu.empty())
        {
            cur=qu.front();
            qu.pop();
            for(int i=0; i<4; i++)
            {
                int xx=cur.x+di[i][0];
                int yy=cur.y+di[i][1];
                if(!go(xx,yy)) continue;
                next.x=xx,next.y=yy,next.t=cur.t+1;
                if(mp[xx][yy]=='x') next.t+=1;
                if(vis[xx][yy])//已经到达过,比较时间是否缩短
                {
                    if(vis[xx][yy]>next.t)
                    {
                        vis[xx][yy]=next.t;
                        qu.push(next);
                    }
                }
                else
                {
                    vis[xx][yy]=next.t;
                    qu.push(next);
                }
    
            }
        }
    }
    int main()
    {
        while(cin>>n>>m)
        {
            for(int i=1; i<=n; i++)
            {
                scanf("%s",mp[i]+1);
                for(int j=1; j<=m; j++)
                {
                    if(mp[i][j]=='a')
                        x2=i,y2=j;
                    else if(mp[i][j]=='r')
                        x1=i,y1=j;
                }
            }
            bfs(x1,y1);
            if(vis[x2][y2])
    
            cout<<vis[x2][y2]<<endl;
            else printf("Poor ANGEL has to stay in the prison all his life.
    ");
        }
        return 0;
    }
  • 相关阅读:
    总结与学习DIV+CSS网页布局技巧
    sns.pairplot()
    使用python处理Excel,Excel中一行数据生产一个Excel文件
    sklearn.model_selection
    sklearn.datasets
    sklearn.metrics 模型评估指标
    LightGBM
    对比Node.js和Python 帮你确定理想编程解决方案!
    小白需要了解的Ajax和websocket的区别以及使用场景!
    为什么越来越多的人想学编程?
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5772547.html
Copyright © 2011-2022 走看看