Advertisement
Sunjaree

Navigate in a 2D Array Smoothly

Oct 29th, 2020 (edited)
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using  namespace  std;
  3.  
  4. #define endl     "\n"
  5. #define ll       long long
  6. #define PI       acos(-1.0)
  7. #define test     cout<<"\n****\n"
  8. #define LCM(a,b) ((a/__gcd(a,b))*b)
  9. #define READ(f)  freopen(f, "r", stdin)
  10. #define WRITE(f) freopen(f, "w", stdout)
  11. #define precise  fixed(cout);cout<<setprecision(20)
  12. #define fast     ios_base :: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  13.  
  14. bool isPrime(int num){
  15.  
  16.     if(num==1 || num==0){
  17.         return false;
  18.     }
  19.  
  20.     for(int i=2;i*i<=num;i++){
  21.         if(num%i==0){
  22.             return false;
  23.         }
  24.     }
  25.     return true;
  26. }
  27.  
  28. #define valid(next_x,next_y) next_x>=0 && next_x<row && next_y>=0 && next_y<column
  29. int fx[]={+1,-1,+0,+0};
  30. int fy[]={+0,+0,+1,-1};                   /*for horizontally and vertically*/
  31.  
  32. int main(){
  33.  
  34.     int row,column;
  35.     cin>>row>>column;
  36.  
  37.     int arr[row][column];
  38.  
  39.     for(int i=0;i<row;i++){
  40.         for(int j=0;j<column;j++){
  41.             cin>>arr[i][j];
  42.         }
  43.     }
  44.  
  45.     cout<<endl;
  46.     for(int i=0;i<row;i++){
  47.         for(int j=0;j<column;j++){
  48.  
  49.             printf("%2d ",arr[i][j]);
  50.         }
  51.         cout<<endl;
  52.     }
  53.  
  54.     cout<<endl;
  55.  
  56.     for(int i=0;i<row;i++){
  57.         for(int j=0;j<column;j++){
  58.  
  59.             if(isPrime(arr[i][j])) {
  60.  
  61.                 for (int k = 0; k < 4; k++) {
  62.  
  63.                     int next_x = i + fx[k];
  64.                     int next_y = j + fy[k];
  65.                     if (valid(next_x, next_y)) {
  66.                         arr[next_x][next_y] = 0;
  67.                     }
  68.                 }
  69.  
  70.             }
  71.         }
  72.     }
  73.  
  74.     for(int i=0;i<row;i++){
  75.         for(int j=0;j<column;j++){
  76.             printf("%2d ",arr[i][j]);
  77.         }
  78.         cout<<endl;
  79.     }
  80.  
  81.     return 0;
  82. }
  83.  
  84. *************************************************************************************
  85. *************************************************************************************
  86.  
  87. #include<bits/stdc++.h>
  88. using  namespace  std;
  89.  
  90. #define endl     "\n"
  91. #define ll       long long
  92. #define PI       acos(-1.0)
  93. #define test     cout<<"\n****\n"
  94. #define LCM(a,b) ((a/__gcd(a,b))*b)
  95. #define READ(f)  freopen(f, "r", stdin)
  96. #define WRITE(f) freopen(f, "w", stdout)
  97. #define precise  fixed(cout);cout<<setprecision(20)
  98. #define fast     ios_base :: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  99.  
  100. bool isPrime(int num){
  101.  
  102.     if(num==1 || num==0){
  103.         return false;
  104.     }
  105.  
  106.     for(int i=2;i*i<=num;i++){
  107.         if(num%i==0){
  108.             return false;
  109.         }
  110.     }
  111.     return true;
  112. }
  113.  
  114. #define valid(next_x,next_y) next_x>=0 && next_x<row && next_y>=0 && next_y<column
  115. int fx[]={+1,-1,+0,+0,-1,+1,-1,+1};
  116. int fy[]={+0,+0,+1,-1,+1,+1,-1,-1};            /* for horizontally,vertically and diagonally*/
  117.  
  118. int main(){
  119.  
  120.     int row,column;
  121.     cin>>row>>column;
  122.  
  123.     int arr[row][column];
  124.  
  125.     for(int i=0;i<row;i++){
  126.         for(int j=0;j<column;j++){
  127.             cin>>arr[i][j];
  128.         }
  129.     }
  130.  
  131.     cout<<endl;
  132.     for(int i=0;i<row;i++){
  133.         for(int j=0;j<column;j++){
  134.  
  135.             printf("%2d ",arr[i][j]);
  136.         }
  137.         cout<<endl;
  138.     }
  139.  
  140.     cout<<endl;
  141.  
  142.     for(int i=0;i<row;i++){
  143.         for(int j=0;j<column;j++){
  144.  
  145.             if(isPrime(arr[i][j])) {
  146.  
  147.                 for (int k = 0; k < 8; k++) {
  148.  
  149.                     int next_x = i + fx[k];
  150.                     int next_y = j + fy[k];
  151.                     if (valid(next_x, next_y)) {
  152.                         arr[next_x][next_y] = 0;
  153.                     }
  154.                 }
  155.  
  156.             }
  157.         }
  158.     }
  159.  
  160.     for(int i=0;i<row;i++){
  161.         for(int j=0;j<column;j++){
  162.             printf("%2d ",arr[i][j]);
  163.         }
  164.         cout<<endl;
  165.     }
  166.  
  167.     return 0;
  168. }
  169.  
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement