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 emmmm,一个预处理的bfs,开始放在中间更新,写的超级麻烦还过不了,后来预处理过了。根据判断这个点的八个方向是否有火来判断他是否会变成火,不要根据有火来使四周的变成火,否则会出错。
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 105
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
int n, m, k, sx, sy, ex, ey, dx[] = { , , , - }, dy[] = { , -, , }, vis[maxn][maxn];
int tx[] = { , , , -, , , -, - }, ty[] = { , -, , , , -, , - }, mapn[maxn][maxn][maxn];
struct node{
int step, x, y;
};
void bfs() {
node p;
p.x = sx, p.y = sy, p.step = ;
vis[sx][sy] = ;
queue<node> q;
q.push(p);
while( !q.empty() ) {
node now = q.front();
q.pop();
if( now.x == ex && now.y == ey ) {
cout << now.step << endl;
return ;
}
for( int i = ; i < ; i ++ ) {
int xx = now.x + dx[i];
int yy = now.y + dy[i];
if( xx >= && xx < n && yy >= && yy < m && !vis[xx][yy] && mapn[(now.step+)/k][xx][yy] ) {
node tmp;
tmp.x = xx, tmp.y = yy, tmp.step = now.step + ;
q.push(tmp);
vis[xx][yy] = ;
}
}
}
cout << "Impossible" << endl;
}
int main() {
while( cin >> n >> m >> k ) {
if( !n && !m && !k ) {
break;
}
memset( vis, , sizeof(vis) );
for( int i = ; i < n; i ++ ) {
for( int j = ; j < m; j ++ ) {
char t;
cin >> t;
if( t == 's' ) {
mapn[][i][j] = ;
sx = i, sy = j;
} else if( t == 't' ) {
mapn[][i][j] = ;
ex = i, ey = j;
} else if( t == '-' ) {
mapn[][i][j] = ;
} else if( t == 'f' ){
mapn[][i][j] = ;
}
}
}
//预处理
for( int p = ; p < max( n, m ); p ++ ) {
for( int i = ; i < n; i ++ ) {
for( int j = ; j < m; j ++ ) {
mapn[p][i][j] = ;
for( int z = ; z < ; z ++ ) {
int xx = i + tx[z];
int yy = j + ty[z];
if( xx >= && xx < n && yy >= && yy < m && !mapn[p-][xx][yy] ) {
mapn[p][i][j] = ;
}
}
}
}
}
bfs();
}
return ;
}

2018湖南多校第二场-20180407 Barareh on Fire的更多相关文章

  1. 2018湖南多校第二场-20180407 Column Addition

    Description A multi-digit column addition is a formula on adding two integers written like this:

  2. 2019牛客多校第二场 A Eddy Walker(概率推公式)

    2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...

  3. 2018 Multi-University Training Contest 2 杭电多校第二场

    开始逐渐习惯被多校虐orz  菜是原罪 1004  Game    (hdoj 6312) 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 虽然披着 ...

  4. 2019年湖南多校第一场||2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)

    第一场多校就打的这么惨,只能说自己太菜了,还需继续努力啊- 题目链接: GYM链接:https://codeforces.com/gym/101933 CSU链接:http://acm.csu.edu ...

  5. hdu6312 2018杭电多校第二场 1004 D Game 博弈

    Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. 2018牛客多校第二场a题

    一个人可以走一步或者跳x步,但不能连着跳,问到这个区间里有几种走法 考虑两种状态  对于这一点,我可以走过来,前面是怎么样的我不用管,也可以跳过来但是,跳过来必须保证前一步是走的 dp[i][0]表示 ...

  7. 2018杭电多校第二场1003(DFS,欧拉回路)

    #include<bits/stdc++.h>using namespace std;int n,m;int x,y;int num,cnt;int degree[100007],vis[ ...

  8. 2019 湖南多校第一场(2018~2019NCPC) 题解

    解题过程 开场shl过B,C,然后lfw写J,J WA了以后shl写A,但是因为OJ上空间开小WA了,而不是MLE?,J加了特判过了.之后一直在检查A错哪了,直到qt发现问题改了空间,浪费许多时间,但 ...

  9. 2014多校第二场1011 || HDU 4882 ZCC Loves Codefires (贪心)

    题目链接 题意 : 给出n个问题,每个问题有两个参数,一个ei(所要耗费的时间),一个ki(能得到的score).每道问题需要耗费:(当前耗费的时间)*ki,问怎样组合问题的处理顺序可以使得耗费达到最 ...

随机推荐

  1. 传输层的TCP和UDP协议

    作者:HerryLo 原文永久链接: https://github.com/AttemptWeb... TCP/IP协议, 你一定常常听到,其中TCP(Transmission Control Pro ...

  2. Python实现网络多人聊天室

    网络多人聊天室 文件结构: chatroom ├── client.py  # 客户端代码 ├── language.py  # 语言文件 ├── server.py  # 服务端代码 └── set ...

  3. 【Java例题】5.3 线性表的使用

    3.线性表的使用.使用ArrayList模拟一个一维整数数组.数据由Random类随机产生.进行对输入的一个整数进行顺序查找.并进行冒泡排序. package chapter6; import jav ...

  4. Elasticsearch实战 | 必要的时候,还得空间换时间!

    1.应用场景 实时数据流通过kafka后,根据业务需求,一部分直接借助kafka-connector入Elasticsearch不同的索引中. 另外一部分,则需要先做聚类.分类处理,将聚合出的分类结果 ...

  5. 从Maven私服获取依赖

    通过Internet直接从Maven公用仓库获取依赖包是默认配置.不过对于中国软件公司来讲,访问这些公用仓库通常较慢,对于一些管理严格的不能直接上网的软件公司来讲,这更加是不可能的.Maven项目可以 ...

  6. Spring Boot Security Oauth2之客户端模式及密码模式实现

    Spring Boot Security Oauth2之客户端模式及密码模式实现 示例主要内容 1.多认证模式(密码模式.客户端模式) 2.token存到redis支持 3.资源保护 4.密码模式用户 ...

  7. ABAP:如何等待小数秒数

    WAIT UP TO x SECONDS. 和CALL FUNCTION 'ENQUE_SLEEP'都只能支持整数的秒数(如果是非整数,则四舍五入),如果要WAIT非整数的描述,可以如下写法:

  8. 洛谷 P2157 [SDOI2009]学校食堂

    题意简述 每个人有一个口味,食堂每次只能为一个人做菜 做每道菜所需的时间是和前一道菜有关的,若前一道菜的对应的口味是a,这一道为b,则做这道菜所需的时间为a 异或 b 每个人都有一个容忍度,最多允许紧 ...

  9. 探究光线追踪技术及UE4的实现

    目录 一.光线追踪概述 1.1 光线追踪是什么 1.2 光线追踪的特点 1.3 光线追踪的历史 1.4 光线追踪的应用 二.光线追踪的原理 2.1 光线追踪的物理原理 2.2 光线追踪算法 2.3 R ...

  10. java后端_百度二面

    参考: https://www.nowcoder.com/discuss/215891?type=2&order=0&pos=10&page=1 1. gc 2. java l ...