博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2018湖南多校第二场-20180407 Barareh on Fire
阅读量:6973 次
发布时间:2019-06-27

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

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 1 stf-2 2 2stf-0 0 0

Sample Output

4ImpossibleImpossible1 emmmm,一个预处理的bfs,开始放在中间更新,写的超级麻烦还过不了,后来预处理过了。根据判断这个点的八个方向是否有火来判断他是否会变成火,不要根据有火来使四周的变成火,否则会出错。
#include
#include
#include
#include
#include
#include
#include
#include
#define maxn 105#define debug(a) cout << #a << " " << a << endlusing namespace std;typedef long long ll;int n, m, k, sx, sy, ex, ey, dx[] = { 0, 0, 1, -1 }, dy[] = { 1, -1, 0, 0 }, vis[maxn][maxn];int tx[] = { 0, 0, 1, -1, 1, 1, -1, -1 }, ty[] = { 1, -1, 0, 0, 1, -1, 1, -1 }, mapn[maxn][maxn][maxn];struct node{ int step, x, y;};void bfs() { node p; p.x = sx, p.y = sy, p.step = 0; vis[sx][sy] = 1; queue
q; q.push(p); while( !q.empty() ) { node now = q.front(); q.pop(); if( now.x == ex && now.y == ey ) { cout << now.step << endl; return ; } for( int i = 0; i < 4; i ++ ) { int xx = now.x + dx[i]; int yy = now.y + dy[i]; if( xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && mapn[(now.step+1)/k][xx][yy] ) { node tmp; tmp.x = xx, tmp.y = yy, tmp.step = now.step + 1; q.push(tmp); vis[xx][yy] = 1; } } } cout << "Impossible" << endl;}int main() { while( cin >> n >> m >> k ) { if( !n && !m && !k ) { break; } memset( vis, 0, sizeof(vis) ); for( int i = 0; i < n; i ++ ) { for( int j = 0; j < m; j ++ ) { char t; cin >> t; if( t == 's' ) { mapn[0][i][j] = 1; sx = i, sy = j; } else if( t == 't' ) { mapn[0][i][j] = 1; ex = i, ey = j; } else if( t == '-' ) { mapn[0][i][j] = 1; } else if( t == 'f' ){ mapn[0][i][j] = 0; } } } //预处理 for( int p = 1; p < max( n, m ); p ++ ) { for( int i = 0; i < n; i ++ ) { for( int j = 0; j < m; j ++ ) { mapn[p][i][j] = 1; for( int z = 0; z < 8; z ++ ) { int xx = i + tx[z]; int yy = j + ty[z]; if( xx >= 0 && xx < n && yy >= 0 && yy < m && !mapn[p-1][xx][yy] ) { mapn[p][i][j] = 0; } } } } } bfs(); } return 0;}

 

转载于:https://www.cnblogs.com/l609929321/p/8746960.html

你可能感兴趣的文章
cordova封装h5为app,cookie不可用解决方法
查看>>
mysql limit 优化
查看>>
谈恋爱与形象
查看>>
day22 Pythonpython 本文xml模块
查看>>
Fisher's exact test( 费希尔精确检验)
查看>>
SSH项目里面 忘记密码的邮件发送功能
查看>>
对Linux(Unix)的基础知识归纳
查看>>
自定义可扩展叠加头部的下拉控件
查看>>
转--Java工程师成神之路(2018修订版)
查看>>
SQLServer之Compute/ComputeBy实现数据汇总
查看>>
KMP算法
查看>>
回车提交
查看>>
代码基本素质
查看>>
关于数据库的索引
查看>>
Apache POI使用详解(摘抄)
查看>>
PAT_A1076#Forwards on Weibo
查看>>
黑马程序员博客-------面向对象
查看>>
Python 类 面向对象(Classes)
查看>>
如何完全卸载(Mac&Windows)office 365 ProPlus
查看>>
android实战开发02
查看>>