zoukankan      html  css  js  c++  java
  • Fire逃生

    Description:  

      You are trapped in a building consisting of open spaces and walls. Some places are on fire and you have to run for the exit. Will you make it?
    At each second, the fire will spread to all open spaces directly connected to the North, South, East or West side of it. Fortunately, walls will never catch fire and will keep the fire inside the building, so as soon as you are out of the building you will be safe. To run to any of the four open spaces adjacent to you takes you exactly one second. You cannot run through a wall or into an open space that is on fire or is just catching fire, but you can run out of an open space at the same moment it catches fire.
      Given a map of the building, decide how fast you can exit the building.

    Input:

      On the first line one positive number: the number of test cases, at most 100. After that per test case:

      one line with two space-separated integers w and h (1 <= w, h <= 1 000): the width and height of the map of the building, respectively.
      h lines with w characters each: the map of the building, consisting of
      – ‘.’: a room,
      – ‘#’: a wall,
      – ‘@’: your starting location,
      – ‘*’: fire.
      There will be exactly one ‘@’ in the map.

    Output:

      Per test case:one line with a single integer which is the minimal number of seconds that you need to exit the building or the string “IMPOSSIBLE” when this is not possible.

    思路:用两个队列分别存放人和大火所在位置,然后从队列取出,对每个‘@’和‘*’所在的点进行四个方向的拓展,对于‘@’,遇到‘.’则将‘.’替换成‘@’,并推进队列,表示人走过的安全点;对于‘*’,遇到‘.’和‘@’都替换成‘*’,并推进队列,表示火蔓延到该点。程序停止的条件为人已安全走出建筑物或被大火蔓延消失鸟。略挫,供参考=o=

    /*0.28sec    4236 KB    3179 Bytes*/
    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    
    #define MAX 1010
    typedef pair<int,int> Location;
    
    char room[MAX][MAX];
    bool visit[MAX][MAX];
    
    int dirt[4][2] = { {-1,0}, {0,1}, {1,0}, {0,-1}};
    int x,y,costTime;
    queue<Location> fire;
    queue<Location> man;
    
    bool safeOrNot(int w,int h)
    {
        bool safe = false;
        costTime = 1;
        while(1)
        {
            int man_Size = man.size();
            int fire_Size = fire.size();
            for(int k = 0; k < fire_Size; k++)
            {
                Location cur = fire.front();
                fire.pop();
                for(int p = 0; p < 4; p++)
                {
                    x = cur.first + dirt[p][0]; 
                    y = cur.second + dirt[p][1]; 
                    if( x >= 0 && x < h && y >= 0 && y < w && !visit[x][y])
                    {
                        if(room[x][y] == '.')
                        {
                            room[x][y] = '*';
                            //cout <<"room["<< x <<"]["<<y<<"] = '.'"<<endl;
                            fire.push(make_pair(x,y));
                            visit[x][y] = true;
                        }
                    }
                    else if(visit[x][y] && room[x][y] == '@')
                    {
                        room[x][y] = '*';
                        fire.push(make_pair(x,y));
                    }
                }       
            }
            for(int i = 0; i < man_Size; i++)
            {
                Location tem = man.front();
                man.pop();
                for(int j = 0; j < 4; j++)
                {
                    x = tem.first + dirt[j][0]; 
                    y = tem.second + dirt[j][1]; 
                    if( x >= 0 && x < h && y >= 0 && y < w && !visit[x][y])
                    {
                        if(room[x][y] == '.')
                        {
                            room[x][y] = '@';
                            man.push(make_pair(x,y));
                            visit[x][y] = true;
                        }
                        else
                            continue;
                    }
                    else if(x < 0 || x >= h || y< 0 || y >= w)
                    {
                        safe = true;
                        return safe;
                    }
                }
            }
            costTime ++;
            if(man.empty())
                return safe;
        }
    }
    
    int main()
    {
        int T,w,h;
        cin >> T;
        while( T-- )
        {
            cin >> w >> h;
            memset(room, 0, sizeof(room));
            memset(visit,false,sizeof(visit));
            while(!fire.empty())
            {
                fire.pop();
            }
            while(!man.empty())
            {
                man.pop();
            }
            for(int i = 0; i < h; i++)
            {
                for(int j = 0; j < w; j++)
                {
                    cin >> room[i][j];
                    if(room[i][j] == '.')
                        visit[i][j] = false;
                    else
                        visit[i][j] = true;
                    if(room[i][j] == '@')
                        man.push(make_pair(i,j));
                    if(room[i][j] == '*')
                        fire.push(make_pair(i,j));
                }
            }
            if(safeOrNot(w,h) )
                cout << costTime << endl;
            else
                cout << "IMPOSSIBLE" << endl;
        }
        //system("pause");
        return 0;
    }                                 
  • 相关阅读:
    【Codeforces】Codeforces Round #680 Div2
    PS1 长命令回到行首进行覆盖
    vue 跟路径加载缺少跟前缀
    Mac OS Virtualbox 倒入 ova 镜像文件
    笔记本电脑扩展屏幕或设备后不能播放声音
    git clone 后使用子分支
    laravel 环境自编译过程
    virtual Box centos7 公司网络环境下不能联网的解决方案
    CentOS7 php7 安装 curl 扩展
    CentOS 7 安装 Nodejs npm 及版本冲突解决
  • 原文地址:https://www.cnblogs.com/chenbjin/p/3161922.html
Copyright © 2011-2022 走看看