Advertisement
Josif_tepe

Untitled

Oct 31st, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int a,b;
  8.     cin>>a>>b;
  9.     int mat[a][b];
  10.     bool poseten[a][b];
  11.     queue<int>Q;
  12.     for(int i=0;i<a;i++)
  13.     {
  14.         for(int j=0;j<b;j++)
  15.         {
  16.             cin>>mat[i][j];
  17.             poseten[i][j]=false;
  18.         }
  19.     }
  20.     int kolku=0;
  21.     int di[] = {-1, 1, 0, 0};
  22.     int dj[] = {0, 0, -1, 1};
  23.    
  24.     for(int k=0;k<=a*b;k++)
  25.     {
  26.         int najmal=40;
  27.         int r=0,c=0;
  28.         for(int i=0;i<a;i++)
  29.         {
  30.             for(int j=0;j<b;j++)
  31.             {
  32.                 if(najmal>mat[i][j] && poseten[i][j]!=true)
  33.                 {
  34.                     najmal=mat[i][j];
  35.                     r=i;
  36.                     c=j;
  37.                 }
  38.             }
  39.         }
  40.  
  41.         if(najmal!=40)
  42.         {
  43.             poseten[r][c]=true;
  44.             Q.push(r);
  45.             Q.push(c);
  46.             kolku++;
  47.             while(!Q.empty())
  48.             {
  49.                 int ci=Q.front();
  50.                 Q.pop();
  51.                 int cj=Q.front();
  52.                 Q.pop();
  53.                 for(int o = 0; o < 4; o++) {
  54.                     int ti = ci + di[o];
  55.                     int tj = cj + dj[o];
  56.                     if(ti >= 0 and ti < a and tj >= 0 and tj < b and !poseten[ti][tj] and mat[ti][tj] >= mat[ci][cj]) {
  57.                         poseten[ti][tj] = true;
  58.                         Q.push(ti);
  59.                         Q.push(tj);
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65.     cout<<kolku;
  66.     return 0;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement