JosepRivaille

P64555: F006B. Distància de Manhattan

May 10th, 2015
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7.  
  8. struct Punt {
  9.     int x, y;
  10. };
  11.  
  12. int dist(const Punt& p) {
  13.     return abs(p.x) + abs(p.y);
  14. }
  15.  
  16. bool comp(const Punt& p1, const Punt& p2) {
  17.     int d1 = dist(p1);
  18.     int d2 = dist(p2);
  19.     if (d1 != d2) return d1 < d2;
  20.     if (p1.x != p2.x) return p1.x < p2.x;
  21.     return p1.y < p2.y;
  22. }
  23.  
  24.  
  25. //Pre: Llegeix seqüències de punts
  26. //Post: Escriu els punts ordenats per distancia al primer creixentment
  27. int main() {
  28.     int x, y, n;
  29.     cin >> x >> y >> n;
  30.     vector<Punt> v(n);
  31.     for (int i = 0; i < n; ++i) {
  32.         int a, b;
  33.         cin >> a >> b;
  34.         v[i].x = a - x;
  35.         v[i].y = b - y;
  36.     }
  37.    
  38.     sort(v.begin(),v.end(),comp);
  39.    
  40.     int d_pre = -1;
  41.     for (int i = 0; i < n; ++i) {
  42.         int d = dist(v[i]);
  43.         if (d > d_pre) {
  44.             cout << "punts a distancia " << d << endl;
  45.             d_pre = d;
  46.         }
  47.         cout << v[i].x + x << ' ' << v[i].y + y << endl;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment