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. SpringMVC项目案例之---数据的获取与显示

    数据的获取与显示 (一)功能 1.对用户输入的数据进行获取 2.将获取的数据显示到页面 3.使用了SpringMVC技术的注解方式 4.使用了过滤器,处理中文乱码问题 5.在web.xml中设置了访问 ...

  2. Unity场景和代码合并以及UnityYAMLMerge的使用

    1.首先是.gitignore的配置. # Folder config file Desktop.ini # Recycle Bin used on file shares $RECYCLE.BIN/ ...

  3. SonarQube系列一、Linux安装与部署

    [前言] 随着项目团队规模日益壮大,项目代码量也越来越多.且不说团队成员编码水平层次不齐,即便是老手,也难免因为代码量的增加和任务的繁重而忽略代码的质量,最终的问题便是bug的增多和代码债务的堆积.因 ...

  4. jQuery发送Ajax请求以及出现的问题

    普通jQuery的Ajax请求代码如下: $.ajax({ type: 'POST', url: "http://xxx/yyy/zzz/sendVerifyCode", data ...

  5. 利用cookie实现浏览器中多个标签页之间的通信

    原理: cookie是浏览器端的存储容器,而且它是多页面共享的,利用cookie多页面共享的特性,可以实现多个标签页的通信. 比如: 一个标签页发送消息(将发送的消息设置到cookie中),一个标签页 ...

  6. HashMap这些问题你知道吗?

    HashMap是Java面试中的常考点之一,而且其<Key,Value>结构也是开发中常常用到的结构之一.或许你使用过HashMap,但是你知道下面这些问题吗? HashMap的底层结构是 ...

  7. 自己动手实现MQTT协议

    写在前面 前段时间弄IoT相关的东西,系统学习了一下 MQTT 协议,在此分享出来. 本文先是对 MQTT 协议做了简单的介绍:接着是对 MQTT协议的内容做了较为全面的解读:最后使用 Python ...

  8. Django2.2中间件详解

    中间件是 Django 用来处理请求和响应的钩子框架.它是一个轻量级的.底层级的"插件"系统,用于全局性地控制Django 的输入或输出,可以理解为内置的app或者小框架. 在dj ...

  9. java后端_百度二面

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

  10. Day 01--选题与设计(一)

    1.第一天我们主要确定了软件课设的项目,做一个学校内食堂订送餐的微信小程序.我们大体的设计思路是:可以实现学生身份的认证,幷使学生们能自行选择校园内的食堂,挑选各个食堂各个窗口菜谱上可以选择的菜,选择 ...