Advertisement
fireLUFFY

PrintingPatterns_Hackerearth

Feb 22nd, 2022 (edited)
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int dx[8]={1,1,0,-1,-1,-1,0,1}; //direction vectors
  5. int dy[8]={0,1,1,1,0,-1,-1,-1}; //direction vectors
  6. int r,c,cx,cy;
  7.  
  8. bool valid(int cx,int cy)
  9. {
  10.     if(cx<0 || cx>=r || cy<0 || cy>=c)
  11.         return false;
  12.     return true;
  13. }
  14.  
  15. main()
  16. {
  17.     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  18.     cin>>r>>c>>cx>>cy;
  19.     int val=0;
  20.  
  21.     vector<vector<int>>mat(r,vector<int>(c));
  22.     vector<vector<bool>>vis(r,vector<bool>(c,false));
  23.  
  24.     queue<pair<int,int>>q;
  25.     int x,y,sz;pair<int,int>p;
  26.     mat[cx][cy]=val;
  27.     vis[cx][cy]=true;
  28.     q.push({cx,cy});
  29.     sz=1;val++;
  30.  
  31.     while(!q.empty())
  32.     {
  33.         p=q.front();
  34.         x=p.first;y=p.second;
  35.         q.pop();sz--;
  36.  
  37.         for(int i=0;i<8;i++){
  38.             if(valid(x+dx[i],y+dy[i]) && !vis[x+dx[i]][y+dy[i]]){
  39.                 q.push({x+dx[i],y+dy[i]});
  40.                 vis[x+dx[i]][y+dy[i]]=true;
  41.                 mat[x+dx[i]][y+dy[i]]=val;
  42.             }
  43.         }
  44.         if(sz==0){
  45.             val++;
  46.             sz=q.size();
  47.         }
  48.     }
  49.  
  50.  
  51.     for(int i=0;i<r;i++){
  52.         for(int j=0;j<c;j++){
  53.             cout<<mat[i][j]<<' ';
  54.         }
  55.         cout<<endl;
  56.     }
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement