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结果超时了,应该把所有的f点都放进队列中进行一次bfs)。然后对人所在的坐标进行bfs,考虑他能到达的每个位置,如果到达该点的时间小于该点发生火灾的时间,则可以到达
#include<cstdio>
#include<string.h>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<algorithm>
using namespace std;
#define INF 1<<28
static const int MAX = 105;
int n, m, k;
int visit[MAX][MAX];
char G[MAX][MAX];
int step[MAX][MAX]; //人移动到该位置的最短时间
int f[MAX][MAX]; //火烧到该位置的最短时间
int Sx, Sy; //人的位置
int Tx, Ty; //直升机位置
void bfs1()
{
int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
queue<pair<pair<int, int>, int> > Q;
//预处理
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (G[i][j] == 'f')
{
Q.push(make_pair(make_pair(i, j), 0));
f[i][j] = 0;
}
while (!Q.empty())
{
pair<pair<int, int>, int> p = Q.front(); Q.pop();
int sx = p.first.first; int sy = p.first.second;
visit[sx][sy] = 1;
for (int i = 0; i < 8; i++)
{
int tx = sx + dx[i];
int ty = sy + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= m)
continue;
if (f[tx][ty] > p.second + k)
f[tx][ty] = p.second + k;
if (visit[tx][ty] == 0)
Q.push(make_pair(make_pair(tx, ty), p.second + k));
}
}
}
int bfs()
{
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
queue<pair<int, int> > Q;
Q.push(make_pair(Sx, Sy));
step[Sx][Sy] = 0;
while (!Q.empty())
{
pair<int, int> p = Q.front(); Q.pop();
visit[p.first][p.second] = 1;
for (int i = 0; i < 4; i++)
{
int tx = p.first + dx[i];
int ty = p.second + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= m)
continue;
if (step[tx][ty] > step[p.first][p.second] + 1 && step[p.first][p.second] + 1 < f[tx][ty])
{
step[tx][ty] = step[p.first][p.second] + 1;
if (visit[tx][ty] == 0)
Q.push(make_pair(tx, ty));
}
}
}
if (step[Tx][Ty] < f[Tx][Ty])
return step[Tx][Ty];
return -1;
}
int main()
{
while (scanf("%d %d %d", &n, &m, &k) && n && m && k)
{
for (int i = 0; i < n; i++)
scanf("%s", G[i]);
memset(visit, 0, sizeof(visit));
for (int i = 0; i < MAX; i++)
for (int j = 0; j < MAX; j++)
{
step[i][j] = INF;
f[i][j] = INF;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
if (G[i][j] == 't')
{
Tx = i; Ty = j;
}
if (G[i][j] == 's')
{
Sx = i; Sy = j;
}
}
bfs1();
memset(visit, 0, sizeof(visit));
int ans = bfs();
if (ans == -1)
printf("Impossible\n");
else
printf("%d\n", ans);
}
return 0;
}