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;
    }
  • 相关阅读:
    AMD 开源照片级渲染引擎 Radeon ProRender
    删除集群mds
    删除集群mds
    一次旅途她和豆浆结缘,如今拥有70多家加盟店
    小伙居然开网店卖花,整整一年时间他做出了400万元的业绩
    带着800元现金开始创业,居然开出了十几家连锁超市
    北大学霸从小米离职,靠眼镜打开亿万市场
    广西农民靠养猪发家致富,采用新模式既有效益又保护生态
    在母婴产品领域他独领风骚,利润已经突破了1亿
    80后小伙白手起家做照明生意,为他创造了六千万元的业绩
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5772547.html
Copyright © 2011-2022 走看看