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 ...
随机推荐
- AngularJS常见面试题
本文引自:https://segmentfault.com/a/1190000005836443 问题来源:如何衡量一个人的 AngularJS 水平? ng-if 跟 ng-show/hide 的区 ...
- ethereum(以太坊)(三)--合约单继承与多继承
pragma solidity ^0.4.0; // priveta public internal contract Test{ //defualt internal uint8 internal ...
- yii2 url 美化参数
所谓的url参数美化就是将冗长的字符串 进行正则替换 yii2 框架的url参数美化完成需要完成两个阶段 第一个阶段分apache和nginx 两种的配置 apache :1.1 必须开启rewrit ...
- hive-pom.xml
4.0.0 <groupId>com.cenzhongman</groupId> <artifactId>hive</artifactId> <v ...
- html+css调用服务器端字体
在浏览网页时,由于客户端没有安装某些特殊字体,导致网页文字无法按设计正常显示,这时我们可以使用服务器字体来避免这种现象的发送 语法 @font-face { /* 自定义字体名称 */ font-fa ...
- Result Maps collection does not contain value for xxxx
这是mybatis查询返回值的错误,我在做一个查询数值的方法,但是我是这样写的: <select id="findSize" resultMap="long&quo ...
- Windows系统安装测试redis
因本人电脑是windows系统,从https://github.com/ServiceStack/redis-windows下载了兼容windows系统的redis 下载后直接解压到D:\redis目 ...
- [网站公告]1月10日1:00-7:00阿里云RDS维护会造成30秒闪断
大家好! 阿里云将于1月10号1:00-7:00(今天夜里)对杭州机房部分RDS实例所在的物理主机做维护操作,维护期间部分RDS实例会有1-2次闪断,每次闪断时间在30秒以内. 我们使用的RDS实例将 ...
- Git上手:Git扫盲区
Git 自述Git 是由伟大的电脑程序员Linus Torvalds编写的一个开源的,分布式的版本控制系统软件. Git 核心原理Git 利用底层数据结构,通过指向索引对象的可变指针,保存文件快照. ...
- Android color颜色-色号总结
code时经常会用到颜色,然而对于像我这样的对于颜色不是很敏感的同学来说,就很痛苦了. 我想要某种颜色,但是又说不出来具体是哪种:这边总结了一下color种类以及色号. <?xml versio ...