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 ...
随机推荐
- HDFS学习指南
本篇HDFS组件基于CDH5进行安装,安装过程:https://www.cnblogs.com/dmjx/p/10037066.html 角色分布 hdp02.yxdev.wx:HDFS server ...
- iar注释快捷键
选中多行后注释快捷键:Ctrl+K 取消多行注释快捷键:Ctrl+Shift+K
- scrapy笔记2
cookies的使用: 使用 scrapy.http.cookie.CookieJar 类的extract_cookies方法,CookieJar._cookies就是我们需要的cookies,是一个 ...
- CERC2017 F: Faulty Factorial 简单数论题
#include <iostream> using namespace std; #define ll long long ; ll n,p,r; ll poww(ll a,ll b){ ...
- 生成heap dump
在查看内存泄露以及对内存问题中,要dump出当前内存堆存储快照,便于分析.有几种方法可以做,简介如下 一.intellij IDEA 由于我用的是intellij IDEA,所以没有介绍Eclipse ...
- PHP.21-商品信息管理
商品信息管理 在线增删改查和图片信息管理 主要技术:文件上传.图片缩放.数据库基本操作 思路: 1.设计并创建数据库 库名:demodb 表名:goods 编号(id) 名称(name) 商品类型(t ...
- Servlet过滤器---编码转换过滤器
该实例用于将请求与相应的编码设置为当前网站的默认编码 java类: import java.io.IOException; import javax.servlet.Filter; import ja ...
- 自动化测试环境搭建--Python及selenium
安装pyhton 访问Python官网:http://www.python.org 下载页Windows下找到适合64位系统的版本 下载后双击安装 安装后查看计算机->属性->高级系统设置 ...
- loadrunner 欺骗ip设置
工具准备:loadrunner12,windows 10 ip欺骗=ip wizard 前提条件:本机IP地址为固定地址,不是自动获取的地址 方法: 1.管理员身份打开cmd 2.输入命令:confi ...
- Canvas 图片平铺设置
/** * 图片平铺 */ function initDemo7(){ var canvas = document.getElementById("demo7"); if (!ca ...