Advertisement
Guest User

Ratos_cegos-problem C

a guest
Apr 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int resp=0;
  4. int M,N;
  5. char mat[1004][1004];
  6. int dx[] = {-1,0,1,0,-1};
  7. int mat2[1004][1004];
  8. void dfs(int x, int y){
  9.     for(int i=0, u,v;i<4;i++){
  10.         u=x+dx[i];
  11.         v=y+dx[i+1];
  12.        
  13.            
  14.         if(u<0 || v<0 || u>=M+2 || v>=N+2) continue;
  15.         if(u<1 || v<1 || u>M || v>N)
  16.             mat[u][v]='.';
  17.        
  18.         if(mat[u][v]=='#' && mat2[u][v]!=1){
  19.             resp++;
  20.             mat2[u][v]=1;
  21.            
  22.         }      
  23.  
  24.         else if(mat[u][v]!='#' && mat2[u][v]!=1){
  25.  
  26.             mat2[u][v]=1;
  27.             dfs(u,v);  
  28.        
  29.         }
  30.        
  31.        
  32.     }
  33.  
  34.  
  35.    
  36. }
  37.  
  38. int main(){
  39.    
  40.     cin>>M>>N;
  41.    
  42.         for(int i=0;i<M;i++){
  43.             for(int j=0;j<N;j++){
  44.                 cin>>mat[i+1][j+1];
  45.             }
  46.         }
  47.         mat2[0][0]=1;
  48.         dfs(0,0);
  49.        
  50.         cout<<resp<<endl;
  51.  
  52.    
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement