CSUOJ2031-Barareh on Fire(双向BFS)

Barareh on Fire

Submit Page    Summary    Time Limit: 3 Sec     Memory Limit: 512 Mb     Submitted: 102     Solved: 48    


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

Hint

 

题解:双向bfs即可,预处理

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<queue>
  5 #include<cmath>
  6 #include<algorithm>
  7 using namespace std;
  8 
  9 struct Node{
 10     int x,y,time;
 11 };
 12 
 13 queue<Node> pq;
 14 int vis[110][110],fire[110][110];
 15 int bfsx[8]={1,-1,0,0,1,1,-1,-1};
 16 int bfsy[8]={0,0,1,-1,1,-1,1,-1};
 17 int n,m,k,min_time;
 18 char Map[110][110];
 19 
 20 void bfsa()
 21 {
 22     Node now,net;
 23     while(!pq.empty())
 24     {
 25         now=pq.front();
 26         pq.pop();
 27         for(int i=0;i<8;i++)
 28         {
 29             net.x=now.x +bfsx[i];
 30             net.y=now.y+bfsy[i];
 31             net.time=now.time+k;
 32             if(net.x>=0&&net.x<n&&net.y>=0&&net.y<m&&!vis[net.x][net.y])
 33             {
 34                 pq.push(net);
 35                 vis[net.x][net.y]=1;
 36                 fire[net.x][net.y]=net.time;    
 37             }
 38         }
 39     }
 40 }
 41 
 42 int bfsb()
 43 {
 44     Node now,net;
 45     while(!pq.empty())
 46     {
 47         now=pq.front();
 48         pq.pop();
 49         for(int i=0;i<4;i++)
 50         {
 51             net.x=now.x +bfsx[i];
 52             net.y=now.y+bfsy[i];
 53             net.time=now.time+1;
 54             
 55             if(net.time<fire[net.x][net.y]&&net.x>=0&&net.x<n&&net.y>=0&&net.y<m&&!vis[net.x][net.y])
 56             {
 57                 if(Map[net.x][net.y]=='t')
 58                     return net.time;
 59                 vis[net.x][net.y]=1;
 60                 pq.push(net);    
 61             }    
 62         }
 63     }
 64     return -1;
 65 }
 66 
 67 int main()
 68 {
 69     while(~scanf("%d%d%d",&n,&m,&k))
 70     {
 71         if(n==0&&m==0&&k==0) break;
 72         int num=0;
 73         while(!pq.empty()) pq.pop();
 74         memset(vis,0,sizeof(vis));
 75         for(int i=0;i<n;i++) scanf("%s",Map[i]);
 76         
 77         Node s,t;
 78         for(int i=0;i<n;i++)
 79         {
 80             for(int j=0;j<m;j++)
 81             {
 82                 if(Map[i][j]=='f')
 83                 {
 84                     fire[i][j]=0;
 85                     vis[i][j]=1;
 86                     pq.push(Node{i,j,0});
 87                     num++;
 88                 }
 89                 if(Map[i][j]=='s') s.x=i,s.y=j,s.time=0;
 90                 if(Map[i][j]=='t') t.x=i,t.y=j;
 91             }
 92         }
 93         
 94         if(num==0)
 95         {
 96             int sum;
 97             sum=fabs(s.x-t.x)+fabs(s.y-t.y);
 98             cout<<sum<<endl;
 99             continue;
100         }
101         
102         bfsa();
103         memset(vis,0,sizeof(vis));
104         while(!pq.empty()) pq.pop();
105         vis[s.x][s.y]=1;
106         pq.push(s);
107         int temp=bfsb();
108         if(temp!=-1) cout<<temp<<endl;
109         else cout<<"Impossible"<<endl;        
110     }
111     
112     return 0;
113 }
114 
115 
116 
117 
118 /**********************************************************************
119     Problem: 2031
120     User: song_hai_lei
121     Language: C++
122     Result: AC
123     Time:12 ms
124     Memory:2256 kb
125 **********************************************************************/
View Code

 

  

posted @ 2018-07-07 22:42  StarHai  阅读(316)  评论(0编辑  收藏  举报