Josif_tepe

Untitled

Feb 8th, 2026
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5. int n,m;
  6. int x[300][300];
  7. bool visited[300][300];
  8. const int di[4]={1,0,-1,0};
  9. const int dj[4]={0,1,0,-1};
  10.  
  11. int bfs(){
  12.     vector<vector<bool>> vis(n, vector<bool>(m, false));
  13.  
  14.     int cnt=0;
  15.     for(int i=0;i<n;i++){
  16.         for(int j=0;j<m;j++){
  17.             if(visited[i][j]==false and !vis[i][j]){
  18.                 int si=i;
  19.                 int sj=j;
  20.                 queue<int>q;
  21.                 q.push(si);
  22.                 q.push(sj);
  23.                 vis[si][sj] = true;
  24.                 while(!q.empty()){
  25.                     int ti=q.front();
  26.                     q.pop();
  27.                     int tj=q.front();
  28.                     q.pop();
  29.                     for(int c=0;c<4;c++){
  30.                         int ci=di[c]+ti;
  31.                         int cj=dj[c]+tj;
  32.                         if(ci>=0 && ci<n && cj>=0 && cj<m && visited[ci][cj]==false and !vis[ci][cj]){
  33.                             q.push(ci);
  34.                             q.push(cj);
  35.                             vis[ci][cj] = true;
  36.                            
  37.                         }
  38.                     }
  39.                 }
  40.                 cnt++;
  41.             }
  42.         }
  43.     }
  44.     return cnt;
  45. }
  46.  
  47. int main()
  48. {
  49.     int k;
  50.     cin>>k;
  51.     int godini[k];
  52.     for(int i=0;i<k;i++){
  53.         cin>>godini[i];
  54.     }
  55.     cin>>n>>m;
  56.     for(int i=0;i<n;i++){
  57.         for(int j=0;j<m;j++){
  58.             cin>>x[i][j];
  59.         }
  60.     }
  61.     for(int i=0;i<n;i++){
  62.         for(int j=0;j<m;j++){
  63.             visited[i][j]=false;
  64.         }
  65.     }
  66.     queue<int>q;
  67.     for(int i=0;i<k;i++){
  68.         for(int j=0;j<n;j++){
  69.             for(int l=0;l<m;l++){
  70.                 if(x[j][l] <= godini[i]){
  71.                     visited[j][l]=true;
  72.                 }
  73.             }
  74.         }
  75.        
  76.         int res=bfs();
  77.         cout<<res<<endl;
  78.     }
  79.    
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment