CSU 2031
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的更多相关文章
- CSU - 2031 Barareh on Fire (两层bfs)
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...
- CSU-ACM2018暑假集训6—BFS
可以吃饭啦!!! A:连通块 ZOJ 1709 Oil Deposits(dfs,连通块个数) B:素数变换 打表+bfs POJ 3216 Prime Path(打表+bfs) C:水bfs HDU ...
- csu 1812: 三角形和矩形 凸包
传送门:csu 1812: 三角形和矩形 思路:首先,求出三角形的在矩形区域的顶点,矩形在三角形区域的顶点.然后求出所有的交点.这些点构成一个凸包,求凸包面积就OK了. /************** ...
- CSU 1503 点到圆弧的距离(2014湖南省程序设计竞赛A题)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 解题报告:分两种情况就可以了,第一种是那个点跟圆心的连线在那段扇形的圆弧范围内,这 ...
- CSU 1120 病毒(DP)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...
- CSU 1116 Kingdoms(枚举最小生成树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...
- CSU 1113 Updating a Dictionary(map容器应用)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...
- CSU 1333 Funny Car Racing (最短路)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1333 解题报告:一个图里面有n个点和m条单向边,注意是单向边,然后每条路开a秒关闭b秒 ...
- CSU 1337 搞笑版费马大定理(2013湖南省程序设计竞赛J题)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 解题报告:虽然x和y的范围都是10^8,但是如果a 是大于1000的话,那么a^3 ...
随机推荐
- python数据类型的转换
- BeanFactory和IOC控制反转
之前在看spring,看IOC实在是云里雾里,包括看AOP也是云里雾里的,后来重新学习Java Web,做了一个简单的web项目,再之后看了崔希凡老师的视频,Day27和Day28两天的内容,真的很有 ...
- SQL tp3.2 批量更新 saveAll
/** * 批量更新数据 * @param [array] $datas [更新数据] * @param [string] $table_name [表名] */ public function sa ...
- Python解压ZIP、RAR等常用压缩格式的方法
解压大杀器 首先祭出可以应对多种压缩包格式的python库:patool.如果平时只用基本的解压.打包等操作,也不想详细了解各种压缩格式对应的python库,patool应该是个不错的选择. pato ...
- bedtools
- MongoDb第一天
安装之后进入cmd.进入到安装目录下的bin目录下. 任意选一个空目录,建立db,log的文件夹.之后终端命令行里面输入回车. D:\ProgramFiles\MongoDB\Server\3.6\b ...
- __builtin_popcount() 函数
详解 该函数的主要作用是计算一个数字的二进制中有多少个1,返回值就是其中1的个数. 它使用一张基于表的方法来进行位搜索,因此这个操作的执行效率很高 此处举一题 P1582 倒水 #include &l ...
- 第一章:Hello, World!
感谢作者 –> 原文链接 本文翻译自The Flask Mega-Tutorial Part I: Hello, World! 一趟愉快的学习之旅即将开始,跟随它你将学会用Python和Flas ...
- (B)springboot配置开发和测试环境并添加启动路径
嗯,开发和测试环境要分离,这是一般共识(虽然我工作过的公司都没有这种分离),spring boot也可以按照配置文件的读取来做到这一点. 上图有三个application开头的配置文件,要达到能够读取 ...
- PYTHON -MYSQLDB安装遇到的问题和解决办法
目前下载的mysqldb在window下没有exe安装包了,只有源码. 使用python setup.py install 命令安装, 报错如下: 异常信息如下: F:\devtools\MySQL- ...