2031: Barareh on Fire

Submit Page   Summary   Time Limit: 3 Sec     Memory Limit: 512 Mb     Submitted: 429     Solved: 119


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 两遍BFS处理,真的考验码力
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,m,k,tim[][],vis[][];
int ddx[]= {,,,-,,,-,-};
int ddy[]= {,-,,,,-,,-};
int dx[]= {,,,-};
int dy[]= {,-,,};
char pic[][];
struct node {
int x,y,step;
};
void bfs1() {
queue<node>p;
node b,now,next;
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(pic[i][j]=='f') {
tim[i][j]=;
b.x=i;
b.y=j;
b.step=;
p.push(b);
}
}
}
while(!p.empty()) {
now=p.front();
p.pop();
for(int d=; d<; d++) {
next=now;
next.x+=ddx[d];
next.y+=ddy[d];
if(next.x<||next.x>=n||next.y<||next.y>=m) continue;
next.step=now.step+k;
if(next.step<tim[next.x][next.y]) {
tim[next.x][next.y]=next.step;
p.push(next);
}
}
}
}
void bfs2(int xx,int yy) {
queue<node>q;
node b,now,next;
b.x=xx;
b.y=yy;
b.step=;
vis[xx][yy]=;
q.push(b);
while(!q.empty()) {
now=q.front();
q.pop();
if(pic[now.x][now.y]=='t') {
printf("%d\n",now.step);
return;
}
for(int d=; d<; d++) {
next=now;
next.x+=dx[d];
next.y+=dy[d];
next.step++;
if(next.x<||next.x>=n||next.y<||next.y>=m) continue;
if(vis[next.x][next.y]!=&&next.step<tim[next.x][next.y]) {
vis[next.x][next.y]=;
q.push(next);
}
}
}
printf("Impossible\n");
}
int main() {
while(scanf("%d%d%d",&n,&m,&k) !=EOF) {
if(n==||m==||k==) break;
getchar();
memset(tim,0x3f3f3f3f,sizeof(tim));
memset(vis,,sizeof(vis));
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
scanf("%c",&pic[i][j]);
}
getchar();
}
bfs1();
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(pic[i][j]=='s') {
bfs2(i,j);
break;
}
}
}
}
return ;
}

CSU 2031的更多相关文章

  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-ACM2018暑假集训6—BFS

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

  3. csu 1812: 三角形和矩形 凸包

    传送门:csu 1812: 三角形和矩形 思路:首先,求出三角形的在矩形区域的顶点,矩形在三角形区域的顶点.然后求出所有的交点.这些点构成一个凸包,求凸包面积就OK了. /************** ...

  4. CSU 1503 点到圆弧的距离(2014湖南省程序设计竞赛A题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 解题报告:分两种情况就可以了,第一种是那个点跟圆心的连线在那段扇形的圆弧范围内,这 ...

  5. CSU 1120 病毒(DP)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...

  6. CSU 1116 Kingdoms(枚举最小生成树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...

  7. CSU 1113 Updating a Dictionary(map容器应用)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...

  8. CSU 1333 Funny Car Racing (最短路)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1333 解题报告:一个图里面有n个点和m条单向边,注意是单向边,然后每条路开a秒关闭b秒 ...

  9. CSU 1337 搞笑版费马大定理(2013湖南省程序设计竞赛J题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 解题报告:虽然x和y的范围都是10^8,但是如果a 是大于1000的话,那么a^3 ...

随机推荐

  1. 在haoodp-2.7.3 HA的基础上安装Hbase HA

    前提安装好hadoop基于QJM的高可用 node1 HMaster node2 HMaster.HRegionServer node3 HRegionServer node4 HRegionServ ...

  2. Java NIO 教程

    Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.本系列教程将有助于你学习和理解Java NIO. Java NIO提供了与 ...

  3. 守护进程,进程安全,IPC进程间通讯,生产者消费者模型

    1.守护进程(了解)2.进程安全(*****) 互斥锁 抢票案例3.IPC进程间通讯 manager queue(*****)4.生产者消费者模型 守护进程 指的也是一个进程,可以守护着另一个进程 一 ...

  4. Pandas 索引和切片

    Series和Datafram索引的原理一样,我们以Dataframe的索引为主来学习 列索引:df['列名'] (Series不存在列索引) 行索引:df.loc[].df.iloc[] 选择列 / ...

  5. opencv中对图像的像素操作

    1.对灰度图像的像素操作: #include<iostream> #include<opencv2/opencv.hpp> using namespace std; using ...

  6. __builtin_popcount() 函数

    详解 该函数的主要作用是计算一个数字的二进制中有多少个1,返回值就是其中1的个数. 它使用一张基于表的方法来进行位搜索,因此这个操作的执行效率很高 此处举一题 P1582 倒水 #include &l ...

  7. 关于原生JS获取class,ID等属性的一些封装

    一.传统上获取是通过document.getElementById获取元素的ID属性,通过总结与学习总结一下获取元素class以及id属性的一些封装; 1.创建构造函数,这里面不需要多解释什么:(主要 ...

  8. CSS3实现带阴影的弹球

    实现div上下跳动时,底部阴影随着变化 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  9. 玩转Node.js(二)

    玩转Node.js(二) 先来回顾上次的内容,上一次我们使用介绍了Node.js并写了第一个服务器端的Hello World程序,在这个Hello World程序中,请求自带的http模块并将其赋给h ...

  10. 珍藏版 Python 开发工程师面试试题

    珍藏版 Python 开发工程师面试试题 说明:不拿到几家公司的offer,那就是卑鄙的浪费 一.Python_基础语法 1.可变与不可变类型: 2.浅拷贝与深拷贝的实现方式.区别:deepcopy如 ...