zoukankan      html  css  js  c++  java
  • J

      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
    ");
    106         else
    107             printf("%d
    ",ans);
    108     }
    109     return 0;
    110 }
  • 相关阅读:
    如何把方法(函数)当参数传递
    致加西亚的信 摘录
    算法:C#数组去除重复元素算法研究
    [转帖]SQL SERVER 2005 安全设置
    [转].NET学习网站收集
    C#你真的懂了吗 啥叫引用2
    比IETEST更好用的浏览器兼容性测试软件[绿色]
    [转帖]使用asp.net访问Oracle的方法汇总
    影响力密码 信任你自己
    [转]自动刷新页面的实现方法总结
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/9005201.html
Copyright © 2011-2022 走看看