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. python中字符串的常见操作(一)

    如有字符串: str1 = '192.168.1.1' str2 = 'asdfghjk' str3 = 'Asd fg hj ki' str4 = ' ' str5 = '' 以下是常见操作:# i ...

  2. Linux基础知识回顾

    1.描述计算机的组成及其功能 计算机是由计算机软件系统和计算机硬件系统两大系统组成 计算机硬件组成 1946年数学家冯诺依曼提出,计算机硬件由运算器.控制器.存储器.输入设备和输出设备5大部件组成,如 ...

  3. 分组取topN

    假设有这样一个文件,文件内容如下 class1 class2 class1 class1 class2 class2 class1 class2 class1 class2 要求按照班级分组取出每个班 ...

  4. Robot Framework自动化测试环境搭建

    robotFramework是一个通用的自动化测试框架来进行验收测试和验收测试驱动开发模式,它具有易于使用的表格的测试数据和关键字测试驱动方法,其测试功能可通过实现与python或java的测试库进行 ...

  5. Lombok 使用详解,简化Java编程

    前言 在 Java 应用程序中存在许多重复相似的.生成之后几乎不对其做更改的代码,但是我们还不得不花费很多精力编写它们来满足 Java 的编译需求 比如,在 Java 应用程序开发中,我们几乎要为所有 ...

  6. mysql锁简谈

    1.mysql锁, 作用:解决因资源共享而造成的并发问题. 实例:买最好一件衣服X A: X 买: X加锁----->试衣服……下单……付款……打包….------>X解锁 B: X 买: ...

  7. PostGIS 安装教程(Linux)(二)

    ##接上篇,上篇讲述了Postgresql的安装,此篇介绍postgis的安装 ##附上上篇链接:https://www.cnblogs.com/giser-s/p/11195419.html 二.安 ...

  8. Flex带CheckBox的Tree(修改ItemRenderer)

    此文代码参考了:http://summerofthatyear-gmail-com.iteye.com/blog/326302 在此表示感谢! 前文提到了,实现带CheckBox的Tree有两种方法: ...

  9. Asis CTF 2016 b00ks理解

    ---恢复内容开始--- 最近在学习堆的off by one,其中遇到这道题,萌新的我弄了大半天才搞懂,网上的很多wp都不是特别详细,都得自己好好调试. 首先,这题目是一个常见的图书馆管理系统,虽然我 ...

  10. 在Windows Server 2019通过Docker Compose部署Asp.Net Core

    一.安装Docker Enterprise 安装文档是: https://docs.docker.com/install/windows/docker-ee/ 安装完成后,如下图 二.首先,拉取一个W ...