• 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;
    }
  • 相关阅读:
    【2017.12.02普及组模拟】送快递
    【NOIP2013模拟联考7】OSU
    顺序表元素位置倒置示例c++实现
    c++字符串排序
    JAVA实现四则运算的简单计算器
    JAVA图形小动画之简单行星运动
    JAVA多线程编程
    ege图形库之简单贪吃蛇(c++)
    ege图形库之动画排序
    c语言中一种典型的排列组合算法
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5772547.html
Copyright © 2020-2023  润新知