CSUOJ2031-Barareh on Fire(双向BFS)
Barareh on Fire
Submit Page Summary Time Limit: 3 Sec Memory Limit: 512 Mb Submitted: 102 Solved: 48
Description
The Barareh village is on fire due to the attack of the virtual enemy. Several places are already on fire and the fire is spreading fast to other places. Khorzookhan who is the only person remaining alive in the war with the virtual enemy, tries to rescue himself by reaching to the only helicopter in the Barareh villiage. Suppose the Barareh village is represented by an n × m grid. At the initial time, some grid cells are on fire. If a cell catches fire at time x, all its 8 vertex-neighboring cells will catch fire at time x + k. If a cell catches fire, it will be on fire forever. At the initial time, Khorzookhan stands at cell s and the helicopter is located at cell t. At any time x, Khorzookhan can move from its current cell to one of four edge-neighboring cells, located at the left, right, top, or bottom of its current cell if that cell is not on fire at time x + 1. Note that each move takes one second. Your task is to write a program to find the shortest path from s to t avoiding fire.
Input
There are multiple test cases in the input. The first line of each test case contains three positive integers n, m and k (1 ⩽ n,m,k ⩽ 100), where n and m indicate the size of the test case grid n × m, and k denotes the growth rate of fire. The next n lines, each contains a string of length m, where the jth character of the ith line represents the cell (i, j) of the grid. Cells which are on fire at time 0, are presented by character “f”. There may exist no “f” in the test case. The helicopter and Khorzookhan are located at cells presented by “t” and “s”, respectively. Other cells are filled by “-” characters. The input terminates with a line containing “0 0 0” which should not be processed.
Output
For each test case, output a line containing the shortest time to reach t from s avoiding fire. If it is impossible to reach t from s, write “Impossible” in the output.
Sample Input
7 7 2
f------
-f---f-
----f--
-------
------f
---s---
t----f-
3 4 1
t--f
--s-
----
2 2 1
st
f-
2 2 2
st
f-
0 0 0
Sample Output
4
Impossible
Impossible
1
Hint
题解:双向bfs即可,预处理
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std; struct Node{
int x,y,time;
}; queue<Node> pq;
int vis[][],fire[][];
int bfsx[]={,-,,,,,-,-};
int bfsy[]={,,,-,,-,,-};
int n,m,k,min_time;
char Map[][]; void bfsa()
{
Node now,net;
while(!pq.empty())
{
now=pq.front();
pq.pop();
for(int i=;i<;i++)
{
net.x=now.x +bfsx[i];
net.y=now.y+bfsy[i];
net.time=now.time+k;
if(net.x>=&&net.x<n&&net.y>=&&net.y<m&&!vis[net.x][net.y])
{
pq.push(net);
vis[net.x][net.y]=;
fire[net.x][net.y]=net.time;
}
}
}
} int bfsb()
{
Node now,net;
while(!pq.empty())
{
now=pq.front();
pq.pop();
for(int i=;i<;i++)
{
net.x=now.x +bfsx[i];
net.y=now.y+bfsy[i];
net.time=now.time+; if(net.time<fire[net.x][net.y]&&net.x>=&&net.x<n&&net.y>=&&net.y<m&&!vis[net.x][net.y])
{
if(Map[net.x][net.y]=='t')
return net.time;
vis[net.x][net.y]=;
pq.push(net);
}
}
}
return -;
} int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
if(n==&&m==&&k==) break;
int num=;
while(!pq.empty()) pq.pop();
memset(vis,,sizeof(vis));
for(int i=;i<n;i++) scanf("%s",Map[i]); Node s,t;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(Map[i][j]=='f')
{
fire[i][j]=;
vis[i][j]=;
pq.push(Node{i,j,});
num++;
}
if(Map[i][j]=='s') s.x=i,s.y=j,s.time=;
if(Map[i][j]=='t') t.x=i,t.y=j;
}
} if(num==)
{
int sum;
sum=fabs(s.x-t.x)+fabs(s.y-t.y);
cout<<sum<<endl;
continue;
} bfsa();
memset(vis,,sizeof(vis));
while(!pq.empty()) pq.pop();
vis[s.x][s.y]=;
pq.push(s);
int temp=bfsb();
if(temp!=-) cout<<temp<<endl;
else cout<<"Impossible"<<endl;
} return ;
} /**********************************************************************
Problem: 2031
User: song_hai_lei
Language: C++
Result: AC
Time:12 ms
Memory:2256 kb
**********************************************************************/
CSUOJ2031-Barareh on Fire(双向BFS)的更多相关文章
- UVA - 11624 Fire! 双向BFS追击问题
Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...
- CSU-2031 Barareh on Fire
CSU-2031 Barareh on Fire Description The Barareh village is on fire due to the attack of the virtual ...
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- 双向BFS
转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
随机推荐
- Linux基础知识回顾
1.描述计算机的组成及其功能 计算机是由计算机软件系统和计算机硬件系统两大系统组成 计算机硬件组成 1946年数学家冯诺依曼提出,计算机硬件由运算器.控制器.存储器.输入设备和输出设备5大部件组成,如 ...
- 替换"marquee",实现无缝滚动
js的marquee标签,可以实现元素循环滚动,但是不能无缝连接,要实现“无缝滚动”的效果必须使用js(借鉴百度),思路是使要滚动元素相对位置不断改变,上下滚动就相对top或者bottom,左右滚动就 ...
- Django 项目笔记
Django 环境的搭建 Django 安装 pip install django==2.1.4 Django 创建项目 django-admin startproject mysite Django ...
- 航空概论(历年资料,引之百度文库,PS:未调格式,有点乱)
航空航天尔雅 选择题1. 已经实现了<天方夜谭>中的飞毯设想.—— A——美国2. 地球到月球大约—— C 38 万公里3. 建立了航空史上第一条定期空中路线—— B——德国4. 对于孔明 ...
- hdu 1880 魔咒词典(双hash)
魔咒词典Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- suseoj 1209: 独立任务最优调度问题(动态规划)
1209: 独立任务最优调度问题 时间限制: 1 Sec 内存限制: 128 MB提交: 3 解决: 2[提交][状态][讨论版][命题人:liyuansong] 题目描述 用2台处理机A和B处理 ...
- Bran的内核开发教程(bkerndev)-08 中断服务程序(ISR)
中断服务程序(ISR) 中断服务程序(ISR)用于保存当前处理器的状态, 并在调用内核的C级中断处理程序之前正确设置内核模式所需的段寄存器.而工作只需要15到20行汇编代码来处理, 包括调用C中的 ...
- EFK教程(3) - ElasticSearch冷热数据分离
基于ElasticSearch多实例架构,实现资源合理分配.冷热数据分离 作者:"发颠的小狼",欢迎转载与投稿 目录 ▪ 用途 ▪ 架构 ▪ 192.168.1.51 elasti ...
- 关闭zabbix 告警
1. 到触发器配置界面开启Allow manual close. (可能需要在连接的模板处修改) 2. 永久关闭告警,即disable该触发器.
- webapi接口安全验证
其实跟大多数网上的方法一样,在前端请求头里加token,后台通过拦截器处理token数据,然后两边对比,如果一样就能通过,不一样就返回无权限. 前端测试代码如下: @{ ViewBag.Title = ...