Barareh on Fire
5431: Barareh on Fire
时间限制: 1 Sec 内存限制: 128 MB提交: 321 解决: 62
[提交] [状态] [讨论版] [命题人:admin]
题目描述
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
来源/分类
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <functional> 5 #include <iostream> 6 #include <queue> 7 using namespace std; 8 const int inf=0x3f3f3f3f; 9 const int maxn=110; 10 char mat[maxn][maxn]; 11 int fire[maxn][maxn]; 12 int n,m,k,go[8][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}}; 13 struct node 14 { 15 int x,y,step; 16 node() 17 { 18 step=0; 19 } 20 }now,nx; 21 22 bool check(int x,int y){ 23 if(x>=0&&x<n&&y>=0&&y<m) return true; 24 return false; 25 } 26 void init(int x,int y){ 27 now.x=x,now.y=y,now.step=0; 28 fire[x][y]=0; 29 queue<node>q; 30 q.push(now); 31 while(!q.empty()){ 32 now=q.front(); 33 q.pop(); 34 // if(now.step/k>max(n,m)) return ; 35 for (int i=0;i<8;i++){ 36 int fx=now.x+go[i][0],fy=now.y+go[i][1]; 37 if(check(fx,fy)&&mat[fx][fy]!='f'&&fire[fx][fy]>now.step+k){ 38 fire[fx][fy]=now.step+k; 39 nx.x=fx,nx.y=fy,nx.step=now.step+k; 40 q.push(nx); 41 42 } 43 } 44 } 45 } 46 47 int bfs(int x,int y) 48 { 49 now.x=x,now.y=y,now.step=0; 50 queue<node> q; 51 q.push(now); 52 if(now.step>=fire[now.x][now.y]&&fire[now.x][now.y]>0) return -1; 53 while(!q.empty()){ 54 now=q.front(); 55 q.pop(); 56 if(mat[now.x][now.y]=='t') return now.step; 57 for (int i=0;i<4;i++){ 58 int x=now.x+go[i][0],y=now.y+go[i][1]; 59 if(check(x,y)&&mat[x][y]!='f'&&now.step+1<fire[x][y]){ 60 nx.x=x,nx.y=y,nx.step=fire[x][y]=now.step+1; 61 q.push(nx); 62 } 63 } 64 } 65 return -1; 66 } 67 68 int main() 69 { 70 //freopen("in.txt","r",stdin); 71 while(scanf("%d%d%d",&n,&m,&k),n+m+k){ 72 memset(fire,inf,sizeof(fire)); 73 int x,y; 74 for (int i=0;i<n;i++) { 75 scanf("%s",mat[i]); 76 for (int j=0;mat[i][j];j++) 77 if(mat[i][j]=='s') x=i,y=j; 78 else if(mat[i][j]=='f') init(i,j); 79 } 80 // for (int i=0;i<n;i++) 81 // { 82 // for (int j=0;j<m;j++) 83 // printf("%d ",fire[i][j]); 84 // printf("\n"); 85 // } 86 int ans=bfs(x,y); 87 if(ans<0) printf("Impossible\n"); 88 else printf("%d\n",ans); 89 } 90 return 0; 91 }