J - Fire!

  1 //两遍bfs
  2 #include<bits/stdc++.h>
  3 
  4 using namespace std;
  5 
  6 const int MAXN = 1010;
  7 
  8 int n,m;
  9 char G[MAXN][MAXN];
 10 int fire[MAXN][MAXN];
 11 int Time[MAXN][MAXN];
 12 int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
 13 
 14 struct node{
 15     int x, y;
 16 }last, now;
 17 
 18 void bfs_Fire(){
 19     memset(fire,-1,sizeof(fire));
 20     queue< node > q;
 21     for(int i = 0 ; i<n ; i++){
 22         for(int j = 0 ; j<m ; j++){
 23             if(G[i][j] == 'F'){
 24                 fire[i][j] = 0;
 25                 last.x = i;
 26                 last.y = j;
 27                 q.push(last);
 28             }
 29         }
 30     }
 31     while(!q.empty()){
 32         last = q.front();
 33         q.pop();
 34 
 35         for(int i = 0 ; i<4 ; i++){
 36             now.x = last.x + dir[i][0];
 37             now.y = last.y + dir[i][1];
 38 
 39             if(now.x<0 || now.x>=n || now.y<0 || now.y>=m)
 40                 continue;
 41             if(fire[now.x][now.y]!=-1)
 42                 continue;
 43             if(G[now.x][now.y] == '#')
 44                 continue;
 45 
 46             fire[now.x][now.y] = fire[last.x][last.y] + 1;
 47             q.push(now);
 48         }
 49     }
 50 }
 51 
 52 int bfs(){
 53     queue< node > q;
 54     memset(Time,-1,sizeof(Time));
 55     for(int i = 0 ; i<n ; i++){
 56         for(int j = 0 ; j<m ; j++){
 57             if(G[i][j] == 'J'){
 58                 last.x = i;
 59                 last.y = j;
 60                 q.push(last);
 61                 Time[i][j] = 0;
 62             }
 63         }
 64     }
 65 
 66     while (!q.empty()){
 67         last = q.front();
 68         q.pop();
 69 
 70         if(last.x==0 || last.y==0 || last.x==n-1 || last.y==m-1)
 71             return Time[last.x][last.y]+1;
 72 
 73         for(int i = 0 ; i<4 ; i++){
 74             now.x = last.x+dir[i][0];
 75             now.y = last.y+dir[i][1];
 76 
 77             if(Time[now.x][now.y] != -1)
 78                 continue;
 79             if(now.x<0 || now.x>=n || now.y<0 || now.y>=m)
 80                 continue;
 81             if(G[now.x][now.y] == '#')
 82                 continue;
 83             if(fire[now.x][now.y]!=-1 && Time[last.x][last.y]+1>=fire[now.x][now.y])
 84                 continue;
 85 
 86             Time[now.x][now.y] = Time[last.x][last.y] + 1;
 87             q.push(now);
 88         }
 89     }
 90     return -1;
 91 }
 92 
 93 int main(){
 94     int T;
 95     scanf("%d",&T);
 96     while (T--){
 97         scanf("%d %d",&n,&m);
 98         for(int i = 0 ; i<n ; i++){
 99             scanf("%s",G[i]);
100         }
101         bfs_Fire();
102 
103         int ans = bfs();
104         if(ans == -1)
105             printf("IMPOSSIBLE\n");
106         else
107             printf("%d\n",ans);
108     }
109     return 0;
110 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325690215&siteId=291194637