Advertisement
rikokovalski

Broj izolovanih oblasti

Jul 30th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. int m, n, povrsinaOblasti;
  7. int a[100][100];
  8. void izolovanaOblast(int y, int x)
  9. {
  10.     a[y][x] = 1;
  11.     povrsinaOblasti++;
  12.  
  13.     if((y>0) && (a[y-1][x] == 0))
  14.         izolovanaOblast(y-1, x);
  15.  
  16.     if((x>0) && (a[y][x-1] == 0))
  17.         izolovanaOblast(y, x-1);
  18.  
  19.     if((x<n-1) && (a[y][x+1] == 0))
  20.         izolovanaOblast(y, x+1);
  21.  
  22.     if((y<m-1) && (a[y+1][x] == 0))
  23.         izolovanaOblast(y+1, x);
  24. }
  25.  
  26.  
  27. int main()
  28. {
  29.     cin >> m >> n;
  30.     for (int i=0; i<m; i++)
  31.         for (int j=0; j<n; j++)
  32.             cin >> a[i][j];
  33.  
  34.     int brIzolovanihOblasti = 0;
  35.  
  36.     for (int i=0; i<m; i++)
  37.         for (int j=0; j<n; j++)
  38.             if(a[i][j] == 0)
  39.             {
  40.                 brIzolovanihOblasti++;
  41.                 povrsinaOblasti = 0;
  42.                 izolovanaOblast(i,j);
  43.                 cout << i << ", " << j << "  povrsina: " << povrsinaOblasti << endl;
  44.             }
  45.     cout << brIzolovanihOblasti << endl;
  46.  
  47.     system("pause");
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement