#include <cstdio> #include <vector> #include <algorithm> using namespace std; struct Rect{ int x1, x2, y1, y2; bool operator<(const Rect &r) const { if(x1 < r.x1) return true; if(x1 > r.x1) return false; if(x2 < r.x2) return true; if(x2 > r.x2) return false; if(y1 < r.y1) return true; if(y1 > r.y1) return false; if(y2 < r.y2) return true; if(y2 > r.y2) return false; return false; } }; Rect sum(Rect &r1, Rect &r2){ Rect r3; r3.x1 = min(r1.x1, r2.x1); r3.x2 = max(r1.x2, r2.x2); r3.y1 = min(r1.y1, r2.y1); r3.y2 = max(r1.y2, r2.y2); return r3; } bool cross(Rect &r1, Rect &r2){ int left = max(r1.x1, r2.x1); int right = min(r1.x2, r2.x2); int bottom = max(r1.y1, r2.y1); int top = min(r1.y2, r2.y2); return (left < right && bottom < top); } int main(){ int n; vector<Rect> rects; scanf("%d", &n); for(int i=0; i<n; i++){ Rect r; scanf("%d%d%d%d", &r.x1, &r.x2, &r.y1, &r.y2); rects.push_back(r); } bool again = true; while(again){ int i = 0; again = false; while(i < (int)rects.size()){ Rect &r1 = rects[i]; for(int j=i+1; j<(int)rects.size(); j++) if(cross(r1, rects[j])){ Rect r3 = sum(r1, rects[j]); rects[i] = r3; rects[j] = rects[(int)rects.size()-1]; rects.pop_back(); again = true; --i; break; } ++i; } } printf("%d\n", (int)rects.size()); sort(rects.begin(), rects.end()); for(int i=0; i<(int)rects.size(); i++){ Rect &r = rects[i]; printf("%d %d %d %d\n", r.x1, r.x2, r.y1, r.y2); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; struct Rect{ int x1, x2, y1, y2; bool operator<(const Rect &r) const { if(x1 < r.x1) return true; if(x1 > r.x1) return false; if(x2 < r.x2) return true; if(x2 > r.x2) return false; if(y1 < r.y1) return true; if(y1 > r.y1) return false; if(y2 < r.y2) return true; if(y2 > r.y2) return false; return false; } }; Rect sum(Rect &r1, Rect &r2){ Rect r3; r3.x1 = min(r1.x1, r2.x1); r3.x2 = max(r1.x2, r2.x2); r3.y1 = min(r1.y1, r2.y1); r3.y2 = max(r1.y2, r2.y2); return r3; } bool cross(Rect &r1, Rect &r2){ int left = max(r1.x1, r2.x1); int right = min(r1.x2, r2.x2); int bottom = max(r1.y1, r2.y1); int top = min(r1.y2, r2.y2); return (left < right && bottom < top); } int main(){ int n; vector<Rect> rects; scanf("%d", &n); for(int i=0; i<n; i++){ Rect r; scanf("%d%d%d%d", &r.x1, &r.x2, &r.y1, &r.y2); rects.push_back(r); } bool again = true; while(again){ int i = 0; again = false; while(i < (int)rects.size()){ Rect &r1 = rects[i]; for(int j=i+1; j<(int)rects.size(); j++) if(cross(r1, rects[j])){ Rect r3 = sum(r1, rects[j]); rects[i] = r3; rects[j] = rects[(int)rects.size()-1]; rects.pop_back(); again = true; --i; break; } ++i; } } printf("%d\n", (int)rects.size()); sort(rects.begin(), rects.end()); for(int i=0; i<(int)rects.size(); i++){ Rect &r = rects[i]; printf("%d %d %d %d\n", r.x1, r.x2, r.y1, r.y2); } return 0; } |