Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. using namespace std;
  5. int n,m;
  6. char c[40010];
  7. int s[40010][5];
  8. bool used[40010];
  9.  
  10. void deep (int x);
  11.  
  12. int main ()
  13. {
  14.     //freopen ("input.txt","r",stdin);
  15.     //freopen ("output.txt","w",stdout);
  16.     int i,k = 0,j;
  17.     cin>>m>>n;
  18.  
  19.     for (i = 1; i <= n*m; i ++)
  20.         cin>>c[i];
  21.     if (c[1] != '.')
  22.     {
  23.         if (c[2] != '.')
  24.             s[1][1] = 2;
  25.         if (c[n+1] != '.')
  26.             s[1][2] = n+1;
  27.     }
  28.     if (c[n] != '.')
  29.     {
  30.         if (c[n-1] != '.')
  31.             s[n][1] = 2;
  32.         if (c[n+n] != '.')
  33.             s[n][2] = n+n;
  34.     }
  35.     if (c[n*(m-1)+1] != '.')
  36.     {
  37.         if (c[n*(m-1)+1-n] != '.')
  38.             s[n*(m-1)+1][1] = n*(m-1)+1-n;
  39.         if (c[n*(m-1)+2] != '.')
  40.             s[n*(m-1)+1][2] = n*(m-1)+2;
  41.     }
  42.     if (c[n*m] != '.')
  43.     {
  44.         if (c[n*m-1] != '.')
  45.             s[n*m][1] = n*m-1;
  46.         if (c[n*m-n] != '.')
  47.             s[n*m][2] = n*m-n;
  48.     }
  49.     for (i = 2; i <= n-1; i ++)
  50.         if (c[i] != '.')
  51.         {
  52.             if (c[i-1] != '.')
  53.                 s[i][1] = i-1;
  54.             if (c[i+1] != '.')
  55.                 s[i][2] = i+1;
  56.             if (c[n+i] != '.')
  57.                 s[i][3] = n+i;
  58.         }
  59.     for (i = n+1; i <= n*(m-2)+1; i +=n)
  60.         if (c[i] != '.')
  61.         {
  62.             if (c[i-n] != '.')
  63.                 s[i][1] = i-n;
  64.             if (c[i+n] != '.')
  65.                 s[i][2] = i+n;
  66.             if (c[i+1] != '.')
  67.                 s[i][3] = i+1;
  68.         }
  69.     for (i = n*(m-1)+2; i <= n*m-1; i ++)
  70.         if (c[i] != '.')
  71.         {
  72.             if (c[i-1] != '.')
  73.                 s[i][1] = i-1;
  74.             if (c[i+1] != '.')
  75.                 s[i][2] = i+1;
  76.             if (c[i-n] != '.')
  77.                 s[i][3] = i-n;
  78.         }
  79.     for (i = n*2; i <= n*(m-1); i +=n)
  80.         if (c[i] != '.')
  81.         {
  82.             if (c[i-n] != '.')
  83.                 s[i][1] = i-n;
  84.             if (c[i+n] != '.')
  85.                 s[i][2] = i+n;
  86.             if (c[i-1] != '.')
  87.                 s[i][3] = i-1;
  88.         }
  89.     for (i = n+2; i <= n*(m-1)-1; i ++)
  90.         if ((c[i] != '.') && (i%n != 0) && (i%n != 1))
  91.         {
  92.             if (c[i-1] != '.')
  93.                 s[i][1] = i-1;
  94.             if (c[i+1] != '.')
  95.                 s[i][2] = i+1;
  96.             if (c[i-n] != '.')
  97.                 s[i][3] = i-n;
  98.             if (c[i+n] != '.')
  99.                 s[i][4] = i+n;
  100.         }
  101.  
  102.     j=1;
  103.     while ((c[j] != '#')&&(j <= m*n))
  104.         j++;
  105.     if (j <= m*n)
  106.     {
  107.         deep(j);
  108.         k++;
  109.         for (i = j+1; i <= n*m; i ++)
  110.             if ((used[i] == false) && (c[i] == '#'))
  111.                 {
  112.                     deep(i);
  113.                     k++;
  114.                 }
  115.     }
  116.     cout<<k<<endl;
  117. }
  118. void deep (int x)
  119. {
  120.     int i;
  121.     used[x] = true;
  122.     for (i = 1; i <= n*m; i ++)
  123.         if ((i != x) && (used[i] == false) && ((s[x][1] == i) || (s[x][2] == i) || (s[x][3] == i) || (s[x][4] == i)))
  124.             deep(i);
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement