velimir

Finkimen

Mar 27th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int x, y, i, j, counter=0, h;
  5. int mat[51][51];
  6. void DFS(int a, int b)
  7. {
  8.     int temp;
  9.     temp = mat[a][b];
  10.     mat[a][b] = -1;
  11.     if(mat[a][b+1]>=temp and b+1<=y and mat[a][b+1]!=-1) DFS(a, b+1);
  12.     if(mat[a][b-1]>=temp and b-1>=0 and mat[a][b-1]!=-1) DFS(a, b-1);
  13.     if(mat[a+1][b]>=temp and a+1<=x and mat[a+1][b]!=-1) DFS(a+1, b);
  14.     if(mat[a-1][b]>=temp and a-1>=0 and mat[a-1][b]!=-1) DFS(a-1, b);
  15. }
  16. int main()
  17. {
  18.  
  19.     cin >> x >> y;
  20.     for(i=0; i<x; i++)
  21.         for(j=0; j<y; j++)
  22.             cin >> mat[i][j];
  23.     for(h=1; h<=30; h++)
  24.         for(i=0; i<x; i++)
  25.             for(j=0; j<y; j++)
  26.                 if(mat[i][j]==h)
  27.                 {
  28.                     DFS(i, j);
  29.                     counter++;
  30.                 }
  31.         cout << counter;
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment