问题 : 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
题意:
就是问你能不能从s走到t,如果能输出最小的到达步数是多少,如果不能输出
Impossible,在走的同时,途中的f会向八个方向蔓延,一秒蔓延一圈,如果这个位置在下一秒会变成f,那么就不能向这个位置移动,还有移动的时候只能向相邻位置移动,就是上下左右。
题解:用一个fire数组模拟f的蔓延,记录各个位置最早被f烧到的时间,如果只有一个f,那么他烧满全图的时间最多是100秒,所以设置100次的循环就够了,然后就是bfs裸题。
题目链接:http://exam.upc.edu.cn/problem.php?cid=1326&pid=4
代码:
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
string kkm[105];
int Map[105][105];
int vis[105][105];
int sx,sy,tx,ty;
int dx1[8]= {1,1,1,0,0,-1,-1,-1};
int dy1[8]= {-1,0,1,-1,1,1,0,-1};
int dx2[4]= {0,0,-1,1};
int dy2[4]= {-1,1,0,0};
int n,m,k;
int ans;
struct node
{
int x,y,step;
node(int a,int b,int c)
{
x=a;
y=b;
step=c;
}
};
void fire()
{
int t=0;
for(int kkm=0; kkm<=100; kkm++)
{
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
if(Map[i][j]==t*k)
{
for(int jjc=0; jjc<8; jjc++)
{
int tx=i+dx1[jjc];
int ty=j+dy1[jjc];
if(tx<0||tx>=n||ty<0||ty>=m||(t+1)*k>Map[tx][ty]) continue;
Map[tx][ty]=(t+1)*k;
}
}
}
t++;
}
}
int bfs()
{
queue<node> q;
vis[sx][sy]=1;
q.push(node(sx,sy,0));
while(!q.empty())
{
node t=q.front();
if(t.x==tx&&t.y==ty) return t.step;
q.pop();
for(int i=0; i<4; i++)
{
int tx=t.x+dx2[i];
int ty=t.y+dy2[i];
int tstep=t.step+1;
if(tx<0||tx>=n||ty<0||ty>=m||vis[tx][ty]||Map[tx][ty]<=tstep) continue;
vis[tx][ty]=1;
q.push(node(tx,ty,tstep));
}
}
return inf;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
if(n==m&&n==k&&k==0) break;
ans=inf;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
Map[i][j]=inf;
vis[i][j]=0;
}
for(int i=0; i<n; i++)
{
cin>>kkm[i];
for(int j=0; j<m; j++)
{
if(kkm[i][j]=='f')
{
Map[i][j]=0;
}
else if(kkm[i][j]=='s')
{
sx=i;
sy=j;
}
else if(kkm[i][j]=='t')
{
tx=i;
ty=j;
}
}
}
fire();
ans=bfs();
if(ans<inf) printf("%d\n",ans);
else printf("Impossible\n");
}
return 0;
}