CSU-2031 Barareh on Fire

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

题意

给定一个n*m的矩阵,其中s代表起点,t代表终点,f代表着火的点,着火的点每过k时间就会点燃相邻的八个格子,问能否从起点到达终点,不能输出impossible,能输出最短时间(每一次移动消耗1秒,只能上下左右移动)

题解

先预处理出每个点会在多少秒着火,然后bfs即可

#include<bits/stdc++.h>
#define maxn 150
using namespace std;
const int inf = 1e9;
int n, m, k;
char G[maxn][maxn];
int d[maxn][maxn];
int fire[maxn][maxn]; struct point {
int x, y;
point(int x = 0, int y = 0): x(x), y(y) {}
} s, t;
queue<point> q;
void init() {
int dx[9] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int dy[9] = { -1, 0, 1, -1, 1, -1, 0, 1 };
while (!q.empty()) {
point now = q.front(); q.pop();
for (int i = 0; i < 8; i++) {
point next = point(now.x + dx[i], now.y + dy[i]);
if (next.x > 0 && next.x <= n && next.y > 0 && next.y <= m) {
if (fire[next.x][next.y] > fire[now.x][now.y] + k) {
fire[next.x][next.y] = fire[now.x][now.y] + k;
q.push(next);
}
}
}
}
}
void bfs() {
q.push(s);
int dx[4] = { 0, 0, 1, -1 };
int dy[4] = { -1, 1, 0, 0 };
while (!q.empty()) {
point now = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
point next = point(now.x + dx[i], now.y + dy[i]);
if (next.x > 0 && next.x <= n && next.y > 0 && next.y <= m) {
if (d[now.x][now.y] + 1 >= fire[next.x][next.y]) continue;
if (d[now.x][now.y] + 1 < d[next.x][next.y]) {
d[next.x][next.y] = d[now.x][now.y] + 1;
q.push(next);
}
}
}
}
}
int main() {
while (scanf("%d%d%d", &n, &m, &k) != EOF) {
if (n == 0 && m == 0 && k == 0) break;
for (int i = 1; i <= n; i++) {
scanf("%s", G[i] + 1);
}
for (int i = 1; i <= n; i++) {
fill(d[i] + 1, d[i] + m + 1, inf);
fill(fire[i] + 1, fire[i] + m + 1, inf);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (G[i][j] == 's') {
s.x = i; s.y = j;
d[i][j] = 0;
}
if (G[i][j] == 't') {
t.x = i; t.y = j;
}
if (G[i][j] == 'f') {
fire[i][j] = 0;
q.push(point(i, j));
}
}
}
init();
bfs();
if (d[t.x][t.y] == inf) printf("Impossible\n");
else printf("%d\n", d[t.x][t.y]);
}
return 0;
}
/**********************************************************************
Problem: 2031
User: Artoriax
Language: C++
Result: AC
Time:12 ms
Memory:2352 kb
**********************************************************************/

CSU-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. CSUOJ 2031 Barareh on Fire

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

  3. CSU 2031

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

  4. 2018湖南多校第二场-20180407 Barareh on Fire

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

  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. 初学基础python记录

    1.对于python来说,最重要的就是缩进.相当于其他语言的{}中括号. 2.转义快捷等 alt+p和alt+n来复制上下一行.变量使用时得先赋值,且大小写敏感,遵循变量命名规则.Python还允许用 ...

  2. cesium 加载TMS影像(已经切片)

    TMS影像数据格式 加载影像的代码: var layers = viewer.scene.imageryLayers; var blackMarble = layers.addImageryProvi ...

  3. Liunx开发(Extjs4.1+desktop+SSH2超强视频教程实践)(2)

    然后装eclipse: 为啥默认是搜狗导航: java还没装呢: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downlo ...

  4. UVA Live Achrive 4327 Parade (单调队列,dp)

    容易想到dp[i][j]表示在第i行j个路口的开始走最大高兴值. 每次可以向左走,或者向右边走,然后向北走.(或者直接往北) 向左走到,状态转移为dp[i][j] = dp[i][k] + happy ...

  5. 【BZOJ3668】[NOI2014] 起床困难综合症(位运算思想)

    点此看题面 大致题意: 给定一些位运算操作,让你在\(0\sim m\)范围内选一个初始值,使其在经过这些运算后得到的结果最大. 前置技能:关于位运算 作为一道位运算的题,如果你不知道什么是位运算,那 ...

  6. iptables 防火墙详解

    一:前言   防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种.无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘.而我们的任务就是需要去定义到底防 ...

  7. 题解 CF734A 【Anton and Danik】

    本蒟蒻闲来无事刷刷水题 话说这道题,看楼下的大佬们基本都是用字符 ( char ) 来做的,那么我来介绍一下C++的优势: string ! string,也就是类型串,是C语言没有的,使用十分方便 ...

  8. 逗塔战争TD新人入门图文攻略

    逗塔战争TD新人入门图文攻略   <逗塔战争TD>是一张基于DOTA改编的塔防TD,很多玩家都很喜欢这张图,新手玩家怎么快速上手这张图呢?这张图的玩法和基本规则并不难,下面就为大家带来新人 ...

  9. c++ 循环程序的作业,2017年10月10日作业题。

    作业1: 需求:输出一个由 * 符号所组成的矩形,要求每行有50个 * ,一共需要有60行.使用双重for循环完成. 作业2: 需求:输出一个由 * 符号所组成的三角形,要求第一行一个 * ,第二行 ...

  10. pthread_cancel函数注意事项

    /************************************************** 相关函数: #include <pthread.h> int pthread_can ...