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 2f-------f---f-----f---------------f---s---t----f-3 4 1t--f--s-----2 2 1stf-2 2 2stf-0 0 0
Sample Output
4ImpossibleImpossible1
Hint
题解:双向bfs即可,预处理
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 9 struct Node{ 10 int x,y,time; 11 }; 12 13 queue 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 =0&&net.y =0&&net.x =0&&net.y