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[][] = { {-,}, {,}, {,}, {,-}};
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逃生的更多相关文章
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- CSUOJ 2031 Barareh on Fire
Description The Barareh village is on fire due to the attack of the virtual enemy. Several places ar ...
- CSU - 2031 Barareh on Fire (两层bfs)
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...
- UVA11624 Fire! —— BFS
题目链接:https://vjudge.net/problem/UVA-11624 题解: 坑点:“portions of the maze havecaught on fire”, 表明了起火点不唯 ...
- 关于SequeezeNet中的Fire Module
在论文<SQUEEZENET: ALEXNET-LEVEL ACCURACY WITH 50X FEWER PARAMETERS AND <0.5MB MODEL SIZE>中,作者 ...
- FZU 2150 Fire Game
Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- Fire
Fire 分析: 首先,明确题意:b1,b2,--,bn 交换为b2,--,bn,b1,但这并不是意味着只能从b1开始交换,(这点从样例中可以看出),并且也不意味着交换的必须是连续的一串,可以是几个单 ...
- Android 轻量级输入校验库:Fire Eye
Fire Eye是一款轻量级简单易用的Android校验库. FireEye 2.0 在 1.0 的基础上,全部重写了代码,并优化了架构,性能上和逻辑上都大大提升.只需要几行代码,即可验证用户输入,并 ...
随机推荐
- 如何让DIV在窗口水平和垂直居中
本实例以新文档开始 2 先放置一个div,并且设置class名为aa,赋予它css属性: width:0;height:0;position:fixed;left:50%;rigth:50%;top: ...
- jquery插件理解看这
zepto 插件写法 一个更换背景颜色的小插件 html 1 <div id="box">content</div> javascript 12345678 ...
- mysql一次添加多条记录
inisert into tabale (name,pwd) values ("jom","123"),("tom","123&q ...
- 关于java中super()和this()
在java中this表示当前类中的对象,super则表示父类中的对象.比如当前对象的某个方法,或当前对象的某个成员,你便可以利用this来实现这个目的,当然,this的另一个用途是调用当前对象的另一个 ...
- 经典线程同步 事件Event
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇 一个经典的多线程同步问题> <秒杀多线程第五篇 经典线程同步关键段CS> 上一篇中使用关键段来解决经典的多线程同步互斥问题 ...
- SharePoint表单和工作流 - Nintex篇(二)
博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 试用版获得的示例网站是一个SharePoint 2010 Server版的网站,我们先来看一下Nintex整个一 ...
- 打饭助手之NABC
Need: 同学们在早上跑操后要吃早饭,还有中午打饭时人更是多.常常要排很长的队伍,造成时间的浪费,和焦急的等待.因此我们需要错开打饭的高峰期,来避免打饭排队的悲哀. Approach: 通过获取摄像 ...
- 新版的tomcat里面get请求通过http协议的时候似乎默认是UTF-8的编码了吗?
不在servler.xml的connector中添加URICoding=“UTF-8”,使用默认值一样没有乱码,而添加URICoding=“iso-8859-1”就是乱码了. POST请求还是用iso ...
- 创建MySQL数据库和表(一)
一.启动MySQL服务 1.在Windows操作系统的“服务”中启动,找到你安装MySQL的起的服务名称,我本机服务名的是MySQL. 2.在命令行中用命令启动: A.启动MySQL服务:net st ...
- 《Java7中 下划线的新特性》
//Java7引入了一个新功能:程序员可以在数值中使用下画线,不管是 //整形数值,还是浮点型数值,都可以自由地使用下划线.通过下划线 //分隔,可以更直观的分辨数值中到底有多少位. public c ...