Simple block communication problems

 

 

 

 

 

 

 

         

 

 

 DFS simple search, the number of communication block is obtained

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 350;
 4 char mp[maxn][maxn],flag[maxn][maxn];
 5 int head[4][2]={{0,-1},{1,0},{0,1},{-1,0}};  //四个方向
 6 int n, m ,q, ans;
 7 
 8 void dfs(int x,int y)
. 9  {
 10      MP [X] [Y] = ' 0 ' ; // tag visited, is no longer accessible
 . 11      for ( int I = 0 ; I < . 4 ; I ++ )
 12 is      {
 13 is          int DX = X + head [I] [ 0 ]; // X direction
 14          int Dy = Y + [I] head [ . 1 ]; // Y direction
 15          IF (MP [DX] [Dy] == ' . 1 ' && DX> = . 1 && DX <= Dy && n-> = . 1 && Dy <= M) // search map
 16          {
 . 17              MP [DX] [Dy] =' 0 ' ; // tag visited
 18 is              DFS (DX, Dy); // recursive search
 . 19          } 
 20 is      }
 21 is  }
 22 is  int main ()
 23 is  {
 24      CIN >> >> n- m;
 25      for ( int I = . 1 ; I <= n; I ++ ) // map input n rows and m columns
 26 is          for ( int J = . 1 ; J <= m; J ++ )
 27              CIN >> MP [I] [J];
 28      CIN >> Q;
 29      the while (q - )
 30     {
 31 is          ANS = 0 ;
 32          int X1, Y1, X2, Y2;  
 33 is          CIN X1 >> X2 >> >> >> Y1 Y2;
 34 is          for ( int I = X1; I <= X2; I ++ ) // X1, y1 to x2, the digital y2 region becomes. 1
 35              for ( int J = y1; J <= y2; J ++ )
 36                  MP [I] [J] = ' . 1 ' ;
 37 [  
38 is          for ( int I = . 1 ; I < n-=; I ++ )
 39              for ( int J = . 1 ; J <= m; J ++)
 40                  In Flag [I] [J] = MP [I] [J]; // save the current state
 41 is  
42 is          for ( int I = . 1 ; I <= n-; I ++ ) // search
 43 is          {
 44 is              for ( int J = . 1 ; J <= m; J ++ )
 45              {
 46 is                  IF (MP [I] [J] == ' . 1 ' )
 47                  {
 48                      ANS ++ ; //; plus number of connected blocks. 1
 49                      DFS (I, J);
 50                  }
 51             }
 52 is          }
 53 is  
54 is          for ( int I = . 1 ; I <= n-; I ++ )
 55              for ( int J = . 1 ; J <= m; J ++ )
 56 is                  MP [I] [J] = In Flag [I] [J] ; // search the search before the reduction FIG.
 57 is          the printf ( " % D \ n- " , ANS); // number of output communication block
 58      }
 59      return  0 ;
 60 }

 

 

 

Guess you like

Origin www.cnblogs.com/Edviv/p/11769015.html