Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int x, y, i, j, counter=0, h;
- int mat[51][51];
- void DFS(int a, int b)
- {
- int temp;
- temp = mat[a][b];
- mat[a][b] = -1;
- if(mat[a][b+1]>=temp and b+1<=y and mat[a][b+1]!=-1) DFS(a, b+1);
- if(mat[a][b-1]>=temp and b-1>=0 and mat[a][b-1]!=-1) DFS(a, b-1);
- if(mat[a+1][b]>=temp and a+1<=x and mat[a+1][b]!=-1) DFS(a+1, b);
- if(mat[a-1][b]>=temp and a-1>=0 and mat[a-1][b]!=-1) DFS(a-1, b);
- }
- int main()
- {
- cin >> x >> y;
- for(i=0; i<x; i++)
- for(j=0; j<y; j++)
- cin >> mat[i][j];
- for(h=1; h<=30; h++)
- for(i=0; i<x; i++)
- for(j=0; j<y; j++)
- if(mat[i][j]==h)
- {
- DFS(i, j);
- counter++;
- }
- cout << counter;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment