Exam 5431 //Barareh on Fire
Barareh on Fire
时间限制: 1 Sec 内存限制: 128 MB题目描述
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.
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.
输入
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.
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.
输出
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.
样例输入
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
样例输出
4
Impossible
Impossible
1
提示
题意比较简单,火从f处向周围八个方向扩散,每走一步花费时间k,人从s处向t处走,每走一步花费时间1,火到达过的地方人不能到达。若人能走到t处求最短时间,否则输出Impossible。
思路:分别对火和人跑一遍bfs。(对人跑bfs时记得vis思想!!)
过题代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=110;
char mat[maxn][maxn];
int fire[maxn][maxn];
int n,m,k,go[8][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
struct node
{
int x,y,step;
node()
{
step=0;
}
}now,nx;
bool check(int x,int y){
if(x>=0&&x<n&&y>=0&&y<m) return true;
return false;
}
void init(int x,int y){
now.x=x,now.y=y,now.step=0;
fire[x][y]=0;
queue<node>q;
q.push(now);
while(!q.empty()){
now=q.front();
q.pop();
// if(now.step/k>max(n,m)) return ;
for (int i=0;i<8;i++){
int fx=now.x+go[i][0],fy=now.y+go[i][1];
if(check(fx,fy)&&mat[fx][fy]!='f'&&fire[fx][fy]>now.step+k){
fire[fx][fy]=now.step+k;
nx.x=fx,nx.y=fy,nx.step=now.step+k;
q.push(nx);
}
}
}
}
int bfs(int x,int y)
{
now.x=x,now.y=y,now.step=0;
queue<node> q;
q.push(now);
if(now.step>=fire[now.x][now.y]&&fire[now.x][now.y]>0) return -1;
while(!q.empty()){
now=q.front();
q.pop();
if(mat[now.x][now.y]=='t') return now.step;
for (int i=0;i<4;i++){
int x=now.x+go[i][0],y=now.y+go[i][1];
if(check(x,y)&&mat[x][y]!='f'&&now.step+1<fire[x][y]){
nx.x=x,nx.y=y,nx.step=fire[x][y]=now.step+1;
q.push(nx);
}
}
}
return -1;
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d%d",&n,&m,&k),n+m+k){
memset(fire,inf,sizeof(fire));
int x,y;
for (int i=0;i<n;i++) {
scanf("%s",mat[i]);
for (int j=0;mat[i][j];j++)
if(mat[i][j]=='s') x=i,y=j;
else if(mat[i][j]=='f') init(i,j);
}
// for (int i=0;i<n;i++)
// {
// for (int j=0;j<m;j++)
// printf("%d ",fire[i][j]);
// printf("\n");
// }
int ans=bfs(x,y);
if(ans<0) printf("Impossible\n");
else printf("%d\n",ans);
}
return 0;
}