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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct point{
int x, y,time;
};
int vis[110][110],vis1[110][110], time_[110][110], dir[8][2] = {1,0, 0,1, -1,0, 0,-1, 1,1, 1,-1, -1,1, -1,-1};
char map[110][110];
int m, n, k,time1;
queue<point>q; bool check(int x,int y)
{
if (x >= 0 && y >= 0 && x < n&&y < m)
return true;
else
return false;
} void bfs1()
{
point u, v;
while (!q.empty())
{
u = q.front();
q.pop();
for (int i = 0; i < 8; i++)
{
v.x = u.x + dir[i][0];
v.y = u.y + dir[i][1];
v.time = u.time + k;
if (check(v.x, v.y) && !vis[v.x][v.y])
{
q.push(v);
vis[v.x][v.y] = 1;
time_[v.x][v.y] = v.time;
}
}
}
} bool bfs2()
{
point u, v;
while (!q.empty())
{
u = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
v.x = u.x + dir[i][0];
v.y = u.y + dir[i][1];
v.time = u.time + 1;
if (check(v.x, v.y) && v.time < time_[v.x][v.y]&&!vis1[v.x][v.y])
{
if (map[v.x][v.y] == 't')
{
time1 = v.time;
return true;
}
vis1[v.x][v.y] = 1;
q.push(v);
}
}
}
return false;
}
int main()
{
while (~scanf("%d%d%d", &n, &m, &k))
{
int num = 0;
if (!n&&!m&&!k)
break;
while (!q.empty())
q.pop();
memset(vis, 0, sizeof(vis));
memset(vis1, 0, sizeof(vis1));
for (int i = 0; i < n; i++)
{
scanf("%s", map[i]);
} point u,v;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (map[i][j] == 'f')
{
time_[i][j] = 0;
vis[i][j] = 1;
q.push(point{ i, j,0 });
num++;
}
if (map[i][j] == 's')
{
u.x = i; u.y = j; u.time = 0;
vis1[u.x][u.y] = 1;
}
if (map[i][j] == 't')
{
v.x = i; v.y = j;
}
}
}
if (num == 0)//注意可能没有火!!!!之前比赛的时候死在了这里
{
time1 = abs(v.x - u.x) + abs(v.y - u.y);
printf("%d\n", time1);
continue;
} bfs1();
while (!q.empty())
q.pop(); q.push(u);
if (bfs2())
printf("%d\n", time1);
else
printf("Impossible\n");
}
return 0;
} /**********************************************************************
Problem: 2031
User: leo6033
Language: C++
Result: AC
Time:12 ms
Memory:2308 kb
**********************************************************************/

CSUOJ 2031 Barareh on Fire的更多相关文章

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

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

  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. 2018湖南多校第二场-20180407 Barareh on Fire

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

  4. CSU 2031

    2031: Barareh on Fire Submit Page   Summary   Time Limit: 3 Sec     Memory Limit: 512 Mb     Submitt ...

  5. CSU-ACM2018暑假集训6—BFS

    可以吃饭啦!!! A:连通块 ZOJ 1709 Oil Deposits(dfs,连通块个数) B:素数变换 打表+bfs POJ 3216 Prime Path(打表+bfs) C:水bfs HDU ...

  6. CSUOJ2031-Barareh on Fire(双向BFS)

    Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...

  7. 关于SequeezeNet中的Fire Module

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

  8. FZU 2150 Fire Game

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

  9. Fire

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

随机推荐

  1. 配置SpringBoot-从日志系统配置说起

    大小系统都需要打日志. 系统在不同环境下对日志的配置要求是不一样的 比如 开发本地: 直接输出到控制台 生产环境: 输出到文件或者额外的日志收集系统, 比如 graylog. (本文不探讨具体日志系统 ...

  2. 【蓝桥杯单片机11】单总线温度传感器DS18B20的基本操作

    [蓝桥杯单片机11]单总线温度传感器DS18B20的基本操作 广东职业技术学院 欧浩源 单总线数字温度传感器DS18B20几乎成了各类单片机甚至ARM实验板的标配模块来,在蓝桥杯的往届省赛和国赛中,这 ...

  3. django的缓存,信号,序列化

    一 Django的缓存机制 1.1 缓存介绍 1.缓存的简介 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的 ...

  4. CSV转excel方法

    步骤一:新建excel文件,数据—>自文本,导入文件 步骤二:选择分隔符,下一步 步骤三:勾选分隔符符合,下一步 步骤四:直接下一步,可在预览里看到格式 步骤五:点击确定,等待数据导入

  5. Python概念-定制自己的数据类型(包装)

    包装:python为大家提供了标准数据类型,以及丰富的内置方法,其实在很多场景下我们都需要基于标准数据类型来定制我们自己的数据类型,新增/改写方法,这就用到了我们刚学的继承/派生知识(其他的标准类型均 ...

  6. 【译】第四篇 Replication:事务复制-订阅服务器

    本篇文章是SQL Server Replication系列的第四篇,详细内容请参考原文. 订阅服务器就是复制发布项目的所有变更将传送到的服务器.每一个发布需要至少一个订阅,但是一个发布可以有多个订阅. ...

  7. node.js 基础篇

    日志输出方式 node test.js 2>error.log 1>info.log 如果需要日志文件追加 node test.js 2>>error.log 1>> ...

  8. Computer Vision Resources

    Computer Vision Resources Softwares Topic Resources References Feature Extraction SIFT [1] [Demo pro ...

  9. 【codeforces】【比赛题解】#861 CF Round #434 (Div.2)

    本来是rated,现在变成unrated,你说气不气. 链接. [A]k-凑整 题意: 一个正整数\(n\)的\(k\)-凑整数是最小的正整数\(x\)使得\(x\)在十进制下末尾有\(k\)个或更多 ...

  10. 利用rundll32执行程序的函数执行程序

    1.前言 无意间发现hexacorn这个国外大佬,给出了很多通过rundll32执行DLL中的函数执行程序的方法,思路很灵巧. 2.原理 rundll32加载dll 用法: rundll32 < ...