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)的更多相关文章

  1. UVA - 11624 Fire! 双向BFS追击问题

    Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...

  2. CSU-2031 Barareh on Fire

    CSU-2031 Barareh on Fire Description The Barareh village is on fire due to the attack of the virtual ...

  3. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

  4. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  5. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  6. [转] 搜索之双向BFS

    转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...

  7. 双向BFS

    转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2)  快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...

  8. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

随机推荐

  1. docker——端口映射

    端口映射 1.就是把容器的端口映射为宿主机的一个随机或者特定端口 2.使得外部用户可以访问容器服务 3.本质其实就是在底层做了iptables地址转发 4.出去的流量是做SNAT原地址转发 5.进来的 ...

  2. 微软的分布式应用框架 Dapr

    微服务架构已成为构建云原生应用程序的标准,微服务架构提供了令人信服的好处,包括可伸缩性,松散的服务耦合和独立部署,但是这种方法的成本很高,需要了解和熟练掌握分布式系统.为了使用所有开发人员能够使用任何 ...

  3. Reporting service个人使用经验

    (此文章是从自己写的Word文档里复制过来的,图没有了,文字也有些乱,凑合看吧) 部署环境 Windows server 2012 R2 软件环境 安装完整的sqlsever2012,IIS服务 需要 ...

  4. Python input函数使用

    本文链接:https://www.cnblogs.com/zyuanlbj/p/11905475.html 函数定义 def input(*args, **kwargs): # real signat ...

  5. 初探three.js

    相信大多数选择前端的小伙伴都有一个设计师的梦,今天我来说一说three.js.three.js是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场景,包括了摄影机.光影.材质等各种对象.学习了 ...

  6. VSCode, Django, and Anaconda开发环境集成配置[Windows]

    之前一直是在Ubuntu下进行Python和Django开发,最近换了电脑,把在Virtual Box 下跑的Ubuntu开发机挪过来总是频繁崩溃,索性就尝试把开发环境挪到Windows主力机了. 不 ...

  7. beta week 2/2 Scrum立会报告+燃尽图 01

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9954 一.小组情况 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 韩昊 ...

  8. Python 编程语言要掌握的技能之一:使用数字与字符串的技巧

    最佳实践 1. 少写数字字面量 “数字字面量(integer literal)” 是指那些直接出现在代码里的数字.它们分布在代码里的各个角落,比如代码 del users[0] 里的 0 就是一个数字 ...

  9. 2019-9-28:渗透测试,基础学习,pgp常量,逻辑运算,DNS投毒,笔记

    sunny.exe clientid 隧道ID route -n 查看网关netstat -rn 查看网关 DNS劫持ettercap用来内网渗透测试使用,可以嗅探内网,DNS劫持等攻击1,在攻击者电 ...

  10. echarts对柱状图进行标注,以及取消hover时的阴影

    option = { color: ['#3398DB'], tooltip : { trigger: 'axis', axisPointer : { // 坐标轴指示器,坐标轴触发有效 type : ...