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 ...
随机推荐
- (转)神舟飞船上的计算机使用什么操作系统,为什么是自研发不是 Linux?
中国航天用的SpaceOS主要内容是仿造美国风河系统公司的VxWorks653(653是产品名,并非版本号).先解释为什么用这个系统不用Linux:航天器的内存和CPU都非常弱,弱到什么程度呢:天宫一 ...
- ### Cause: java.lang.reflect.UndeclaredThrowableException
### Cause: java.lang.reflect.UndeclaredThrowableException Caused by: org.apache.ibatis.exceptions.Pe ...
- Leetcode 538. 把二叉搜索树转换为累加树
题目链接 https://leetcode.com/problems/convert-bst-to-greater-tree/description/ 题目描述 大于它的节点值之和. 例如: 输入: ...
- 25-IHostEnvironment和 IApplicationLifetime介绍
//类似 global.ashx的application事件的实现1-Startup类中 public void Configure(IApplicationBuilder app, IHosting ...
- C# 中的正则简单例子
public static void Main() { Regex rgx = new Regex(@"[S|s]et-[C|c]ookie: (?<cookieName>\w+ ...
- Windows GitLab使用全过程
1.首先安装Git 1.1.下载网站: https://git-for-windows.github.io/ 1.2.安装Git参考网站 http://blog.csdn.net/u012614287 ...
- viewpager 无网络的时候滑动异常
不知道大家有没有遇到过这种情况,就是框架是viewpager+fragment的架构.然后呢,fragment里面是webview.一般情况下,当没有网的时候,webviwe会说什么找不到网页,然后很 ...
- Reverse Word in a String(翻转字符串)&字符串最后一个单词的长度
1.题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is ...
- DNSSec
Domain Name System Security Extensions (DNSSEC)DNS安全扩展,是由IETF提供的一系列DNS安全认证的机制(可参考RFC2535).它提供了一种来源鉴定 ...
- 《Cracking the Coding Interview》——第6章:智力题——题目1
2014-03-19 06:40 题目:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药.给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的? 解法:如果药片管 ...