2018湖南多校第二场-20180407 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 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

emmmm,一个预处理的bfs,开始放在中间更新,写的超级麻烦还过不了,后来预处理过了。根据判断这个点的八个方向是否有火来判断他是否会变成火,不要根据有火来使四周的变成火,否则会出错。
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 105
#define debug(a) cout << #a << " " << a << endl
using 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<node> 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