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[][] = { {-,}, {,}, {,}, {,-}};
int x,y,costTime;
queue<Location> fire;
queue<Location> man; bool safeOrNot(int w,int h)
{
bool safe = false;
costTime = ;
while()
{
int man_Size = man.size();
int fire_Size = fire.size();
for(int k = ; k < fire_Size; k++)
{
Location cur = fire.front();
fire.pop();
for(int p = ; p < ; p++)
{
x = cur.first + dirt[p][];
y = cur.second + dirt[p][];
if( x >= && x < h && y >= && 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 = ; i < man_Size; i++)
{
Location tem = man.front();
man.pop();
for(int j = ; j < ; j++)
{
x = tem.first + dirt[j][];
y = tem.second + dirt[j][];
if( x >= && x < h && y >= && 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 < || x >= h || y< || 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, , sizeof(room));
memset(visit,false,sizeof(visit));
while(!fire.empty())
{
fire.pop();
}
while(!man.empty())
{
man.pop();
}
for(int i = ; i < h; i++)
{
for(int j = ; 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 ;
}

Fire逃生的更多相关文章

  1. UVa 11624 Fire!(着火了!)

    UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...

  2. CSUOJ 2031 Barareh on Fire

    Description The Barareh village is on fire due to the attack of the virtual enemy. Several places ar ...

  3. CSU - 2031 Barareh on Fire (两层bfs)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...

  4. UVA11624 Fire! —— BFS

    题目链接:https://vjudge.net/problem/UVA-11624 题解: 坑点:“portions of the maze havecaught on fire”, 表明了起火点不唯 ...

  5. 关于SequeezeNet中的Fire Module

    在论文<SQUEEZENET: ALEXNET-LEVEL ACCURACY WITH 50X FEWER PARAMETERS AND <0.5MB MODEL SIZE>中,作者 ...

  6. FZU 2150 Fire Game

    Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  7. HDU 4857 逃生 (反向拓扑排序 & 容器实现)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  8. Fire

    Fire 分析: 首先,明确题意:b1,b2,--,bn 交换为b2,--,bn,b1,但这并不是意味着只能从b1开始交换,(这点从样例中可以看出),并且也不意味着交换的必须是连续的一串,可以是几个单 ...

  9. Android 轻量级输入校验库:Fire Eye

    Fire Eye是一款轻量级简单易用的Android校验库. FireEye 2.0 在 1.0 的基础上,全部重写了代码,并优化了架构,性能上和逻辑上都大大提升.只需要几行代码,即可验证用户输入,并 ...

随机推荐

  1. Web API初印象

    理解REST,RESTful和Web API 1.REST:Representational State Transfer表征状态转移,是Roy Fielding博士在2000年他的博士论文中提出来的 ...

  2. js继承实例

    第一种方法:对象冒充(临时属性) 借用临时属性,指向超类,末了删除 function Person(name,gender){ this.name=name; this.gender=gender; ...

  3. 转: HTTP协议的头信息详解

    通常HTTP消息包括客户机向服务器的请求消息和服务器向客户机的响应消息.这两种类型的消息由一个起始行,一个或者多个头域,一个只是头域结束的空行和可 选的消息体组成.HTTP的头域包括通用头,请求头,响 ...

  4. HDU 1698 区间更新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. RM报表 文本框 自动换行 相关代码

    procedure TRMCustomMemoView.WrapMemo1(aAddChar: Boolean); var lCurHeight, lOneLineHeight, lMaxWidth: ...

  6. HDU 3377 插头dp

    题目大意: 从左上角走到右下角,每个点之多经过一次,取到所有路径上经过点的权值,求最大的权值之和,这里走到右下角就算停止了 这里有个思路是转化成熟悉的回路问题 在上方和右方最外围定义一圈权值为0 , ...

  7. POJ 3207 2-sat

    题目大意: 在圆上顺时针n个点,给定m个连接,可以通过圆内或者圆外相交,问能不能找到一种方式,使这些连接的边都不相交 这里很容易看出的是,这些边只有在圆外或者圆内两种连接方式,而且必须选择其中一种 所 ...

  8. _x、__x、__x__含义与区别

    _x是一种弱表示,它用在类中的属性或方法,表示是private属性,希望外部使用者不要直接调用它.但它只是暗示,没有任何限制性措施. private属性主要推荐的还是这种方式,因为Python的设计理 ...

  9. [安卓][转]Android eclipse中程序调试

    一:断点调试 用eclipse开发android程序的时,跟VS一样是可以断点单步调试的.步骤如下.1 设置断点:在编码窗体的左边框上用鼠标双击,或者右键点击菜单,选择 Toggle Breakpoi ...

  10. app store 上架流程

    前言:作为一名IOS开发者,把开发出来的App上传到App Store是必须的.下面就来详细介绍下具体流程. 1.打开苹果开发者中心:https://developer.apple.com 打开后点击 ...