11624 - Fire!

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671

bfs预处理出每个点起火时间,然后再bfs出去到时间。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<queue>
  4 #define mt(a,b) memset(a,b,sizeof(a))
  5 using namespace std;
  6 const int inf=0x3f3f3f3f;
  7 const int M=1024;
  8 int t,n,m;
  9 char a[M][M];
 10 int d[M][M];
 11 int dx[]={0,0,1,-1};
 12 int dy[]={1,-1,0,0};
 13 struct Q{
 14     int x,y,step;
 15 }now,pre;
 16 queue<Q> q;
 17 bool inside(const int &x,const int &y){
 18     if(x>=0&&x<n&&y>=0&&y<m) return true; return false;
 19 }
 20 void bfs(){
 21     for(int i=0;i<n;i++){
 22         for(int j=0;j<m;j++){
 23             d[i][j]=inf;
 24         }
 25     }
 26     while(!q.empty()) q.pop();
 27     for(int i=0;i<n;i++){
 28         for(int j=0;j<m;j++){
 29             if(a[i][j]=='F'){
 30                 now.x=i;
 31                 now.y=j;
 32                 now.step=0;
 33                 d[i][j]=0;
 34                 q.push(now);
 35             }
 36         }
 37     }
 38     while(!q.empty()){
 39         pre=q.front();
 40         q.pop();
 41         for(int i=0;i<4;i++){
 42             int tx=pre.x+dx[i];
 43             int ty=pre.y+dy[i];
 44             if(!inside(tx,ty)||a[tx][ty]=='#') continue;
 45             now.step=pre.step+1;
 46             if(d[tx][ty]>now.step){
 47                 d[tx][ty]=now.step;
 48                 now.x=tx;
 49                 now.y=ty;
 50                 q.push(now);
 51             }
 52         }
 53     }
 54 }
 55 bool out(const Q &a){
 56     if(a.x==0||a.x==n-1||a.y==0||a.y==m-1) return true; return false;
 57 }
 58 bool vis[M][M];
 59 int solve(){
 60     mt(vis,0);
 61     while(!q.empty()) q.pop();
 62     for(int i=0;i<n;i++){
 63         for(int j=0;j<m;j++){
 64             if(a[i][j]=='J'){
 65                 vis[i][j]=true;
 66                 now.x=i;
 67                 now.y=j;
 68                 now.step=0;
 69                 q.push(now);
 70             }
 71         }
 72     }
 73     while(!q.empty()){
 74         pre=q.front();
 75         q.pop();
 76         if(out(pre)) return pre.step+1;
 77         for(int i=0;i<4;i++){
 78             int tx=pre.x+dx[i];
 79             int ty=pre.y+dy[i];
 80             if(!inside(tx,ty)||a[tx][ty]=='#'||vis[tx][ty]||d[tx][ty]<=pre.step+1) continue;
 81             vis[tx][ty]=true;
 82             now.x=tx;
 83             now.y=ty;
 84             now.step=pre.step+1;
 85             q.push(now);
 86         }
 87     }
 88     return -1;
 89 }
 90 int main(){
 91     while(~scanf("%d",&t)){
 92         while(t--){
 93             scanf("%d%d",&n,&m);
 94             for(int i=0;i<n;i++){
 95                 scanf("%s",a[i]);
 96             }
 97             bfs();
 98             int ans=solve();
 99             if(~ans) printf("%d\n",ans);
100             else puts("IMPOSSIBLE");
101         }
102     }
103     return 0;
104 }
View Code

相关文章:

  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2021-10-28
  • 2021-11-21
  • 2021-10-04
  • 2022-01-12
  • 2021-07-21
猜你喜欢
  • 2021-07-28
  • 2021-08-19
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案