博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSU-2031 Barareh on Fire
阅读量:5328 次
发布时间:2019-06-14

本文共 4817 字,大约阅读时间需要 16 分钟。

CSU- Barareh on Fire

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 2f-------f---f-----f---------------f---s---t----f-3 4 1t--f--s-----2 2 1stf-2 2 2stf-0 0 0

Sample Output

4ImpossibleImpossible1

题意

给定一个n*m的矩阵,其中s代表起点,t代表终点,f代表着火的点,着火的点每过k时间就会点燃相邻的八个格子,问能否从起点到达终点,不能输出impossible,能输出最短时间(每一次移动消耗1秒,只能上下左右移动)

题解

先预处理出每个点会在多少秒着火,然后bfs即可

#include
#define maxn 150using namespace std;const int inf = 1e9;int n, m, k;char G[maxn][maxn];int d[maxn][maxn];int fire[maxn][maxn];struct point { int x, y; point(int x = 0, int y = 0): x(x), y(y) {}} s, t;queue
q;void init() { int dx[9] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int dy[9] = { -1, 0, 1, -1, 1, -1, 0, 1 }; while (!q.empty()) { point now = q.front(); q.pop(); for (int i = 0; i < 8; i++) { point next = point(now.x + dx[i], now.y + dy[i]); if (next.x > 0 && next.x <= n && next.y > 0 && next.y <= m) { if (fire[next.x][next.y] > fire[now.x][now.y] + k) { fire[next.x][next.y] = fire[now.x][now.y] + k; q.push(next); } } } }}void bfs() { q.push(s); int dx[4] = { 0, 0, 1, -1 }; int dy[4] = { -1, 1, 0, 0 }; while (!q.empty()) { point now = q.front(); q.pop(); for (int i = 0; i < 4; i++) { point next = point(now.x + dx[i], now.y + dy[i]); if (next.x > 0 && next.x <= n && next.y > 0 && next.y <= m) { if (d[now.x][now.y] + 1 >= fire[next.x][next.y]) continue; if (d[now.x][now.y] + 1 < d[next.x][next.y]) { d[next.x][next.y] = d[now.x][now.y] + 1; q.push(next); } } } }}int main() { while (scanf("%d%d%d", &n, &m, &k) != EOF) { if (n == 0 && m == 0 && k == 0) break; for (int i = 1; i <= n; i++) { scanf("%s", G[i] + 1); } for (int i = 1; i <= n; i++) { fill(d[i] + 1, d[i] + m + 1, inf); fill(fire[i] + 1, fire[i] + m + 1, inf); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (G[i][j] == 's') { s.x = i; s.y = j; d[i][j] = 0; } if (G[i][j] == 't') { t.x = i; t.y = j; } if (G[i][j] == 'f') { fire[i][j] = 0; q.push(point(i, j)); } } } init(); bfs(); if (d[t.x][t.y] == inf) printf("Impossible\n"); else printf("%d\n", d[t.x][t.y]); } return 0;}/********************************************************************** Problem: 2031 User: Artoriax Language: C++ Result: AC Time:12 ms Memory:2352 kb**********************************************************************/

转载于:https://www.cnblogs.com/artoriax/p/10351681.html

你可能感兴趣的文章
Windows Phone开发(5):室内装修 转:http://blog.csdn.net/tcjiaan/article/details/7269014
查看>>
详谈js面向对象 javascript oop,持续更新
查看>>
关于这次软件以及pda终端的培训
查看>>
jQuery上传插件Uploadify 3.2在.NET下的详细例子
查看>>
如何辨别一个程序员的水平高低?是靠发量吗?
查看>>
新手村之循环!循环!循环!
查看>>
正则表达式的用法
查看>>
线程安全问题
查看>>
SSM集成activiti6.0错误集锦(一)
查看>>
个人作业
查看>>
下拉刷新
查看>>
linux的子进程调用exec( )系列函数
查看>>
MSChart的研究
查看>>
C# 索引器
查看>>
MySQLdb & pymsql
查看>>
zju 2744 回文字符 hdu 1544
查看>>
delphi 内嵌汇编例子
查看>>
【luogu P2298 Mzc和男家丁的游戏】 题解
查看>>
前端笔记-bom
查看>>
MATLAB作图方法与技巧(一)
查看>>