Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- int main()
- {
- int a,b;
- cin>>a>>b;
- int mat[a][b];
- bool poseten[a][b];
- queue<int>Q;
- for(int i=0;i<a;i++)
- {
- for(int j=0;j<b;j++)
- {
- cin>>mat[i][j];
- poseten[i][j]=false;
- }
- }
- int kolku=0;
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- for(int k=0;k<=a*b;k++)
- {
- int najmal=40;
- int r=0,c=0;
- for(int i=0;i<a;i++)
- {
- for(int j=0;j<b;j++)
- {
- if(najmal>mat[i][j] && poseten[i][j]!=true)
- {
- najmal=mat[i][j];
- r=i;
- c=j;
- }
- }
- }
- if(najmal!=40)
- {
- poseten[r][c]=true;
- Q.push(r);
- Q.push(c);
- kolku++;
- while(!Q.empty())
- {
- int ci=Q.front();
- Q.pop();
- int cj=Q.front();
- Q.pop();
- for(int o = 0; o < 4; o++) {
- int ti = ci + di[o];
- int tj = cj + dj[o];
- if(ti >= 0 and ti < a and tj >= 0 and tj < b and !poseten[ti][tj] and mat[ti][tj] >= mat[ci][cj]) {
- poseten[ti][tj] = true;
- Q.push(ti);
- Q.push(tj);
- }
- }
- }
- }
- }
- cout<<kolku;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement