Advertisement
Fastrail08

Matrix cells in distance order

Aug 21st, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include<vector>
  3. #include<algorithm>
  4.  
  5. #define mp make_pair
  6.  
  7. using namespace std;
  8. bool mySort(const pair<int, int> &a, const pair<int, int> &b, int sr, int sc){
  9.   if((abs(a.first - sr) + abs(a.second - sc)) == (abs(b.first - sr) + abs(b.second - sc))){
  10.     return a.first < b.first ? true : false;
  11.   }
  12.   return (abs(a.first - sr) + abs(a.second - sc)) < (abs(b.first - sr) + abs(b.second - sc)) ? true : false;
  13. }  
  14. class matrixSort{
  15.   public:
  16.   int sr, sc;
  17.     matrixSort(int sr, int sc){
  18.       this-> sr = sr;
  19.       this-> sc = sc;
  20.     }
  21.    
  22.   bool operator()(const pair<int, int> &a, const pair<int, int> &b){
  23.     return mySort(a, b, sr, sc);
  24.   }
  25. };
  26.  
  27.  
  28.  
  29.  
  30. int main()
  31. {
  32.     int r, c, sr, sc;
  33.     cin >> r >> c >> sr >> sc;
  34.     vector<pair<int, int> > v;
  35.     for(int i = 0; i < r; i++)
  36.       for(int j = 0; j < c; j++){
  37.         v.push_back(mp(i, j));
  38.       }
  39.     sort(v.begin(), v.end(), matrixSort(sr, sc));
  40.     for(auto i : v){
  41.       cout << "(" << i.first << ", " << i.second << "), ";
  42.     }
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement