#include <iostream> #include <stdio.h> #include <algorithm> #include <vector> using namespace std; struct prost { int x1, x2; int y1, y2; }; bool cmp1(struct prost a, struct prost b) { if (a.x1 == b.x1 && a.x2 == b.x2 && a.y1 == b.y1) return a.y2 < b.y2; if (a.x1 == b.x1 && a.x2 == b.x2) return a.y1 < b.y1; if (a.x1 == b.x1) return a.x2 < b.x2; return a.x1 < b.x1; } vector<prost> wynik; bool czyPrzeciecie(struct prost& a, struct prost& b) { if (b.x1 <= a.x1 && a.x1 < b.x2 && b.y1 < a.y2 && a.y2 <= b.y2) { return true; } if (b.x1 < a.x2 && a.x2 <= b.x2 && b.y1 <= a.y1 && a.y1 < b.y2) { return true; } if (a.x1 < b.x2 && b.x2 <= a.x2 && a.y1 < b.y2 && b.y2 <= a.y2) { return true; } if (a.x1 <= b.x1 && b.x1 < a.x2 && a.y1 <= b.y1 && b.y1 < a.y2) { return true; } if (b.x1 <= a.x1 && a.x1 < b.x2 && b.y1 <= a.y1 && a.y1 < b.y2) { return true; } if (a.x1 <= b.x1 && b.x1 < a.x2 && a.y1 < b.y2 && b.y2 <= a.y2) { return true; } if (a.x1 <= b.x1 && a.x2 >= b.x2 && b.y1 <= a.y1 && b.y2 >= a.y2) { return true; } if (b.x1 <= a.x1 && b.x2 >= a.x2 && a.y1 <= b.y1 && a.y2 >= b.y2) { return true; } if (a.x1 < b.x2 && b.x2 <= a.x2 && a.y1 < b.y2 && b.y2 <= a.y2) { return true; } if (a.x1 < b.x2 && b.x2 <= a.x2 && a.y1 <= b.y1 && b.y1 < a.y2) { return true; } return false; } void dodajDoWyniku(struct prost& a) { bool dodano = true; for (int i = 0; i < wynik.size() && dodano; i++) { if (czyPrzeciecie(wynik[i], a)) { struct prost nowy; nowy.x1 = min(a.x1, wynik[i].x1); nowy.x2 = max(a.x2, wynik[i].x2); nowy.y1 = min(a.y1, wynik[i].y1); nowy.y2 = max(a.y2, wynik[i].y2); wynik.erase(wynik.begin() + i); dodajDoWyniku(nowy); dodano = false; } } if (dodano) wynik.push_back(a); } int main() { int n; scanf("%d", &n); int x1, x2, y1, y2; for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &x1, &x2, &y1, &y2); struct prost nowyProstokat; nowyProstokat.x1 = x1; nowyProstokat.x2 = x2; nowyProstokat.y1 = y1; nowyProstokat.y2 = y2; dodajDoWyniku(nowyProstokat); } sort(wynik.begin(), wynik.end(), cmp1); printf("%d\n", wynik.size()); for (int i = 0; i < wynik.size(); i++) { printf("%d %d %d %d\n", wynik[i].x1, wynik[i].x2, wynik[i].y1, wynik[i].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 93 | #include <iostream> #include <stdio.h> #include <algorithm> #include <vector> using namespace std; struct prost { int x1, x2; int y1, y2; }; bool cmp1(struct prost a, struct prost b) { if (a.x1 == b.x1 && a.x2 == b.x2 && a.y1 == b.y1) return a.y2 < b.y2; if (a.x1 == b.x1 && a.x2 == b.x2) return a.y1 < b.y1; if (a.x1 == b.x1) return a.x2 < b.x2; return a.x1 < b.x1; } vector<prost> wynik; bool czyPrzeciecie(struct prost& a, struct prost& b) { if (b.x1 <= a.x1 && a.x1 < b.x2 && b.y1 < a.y2 && a.y2 <= b.y2) { return true; } if (b.x1 < a.x2 && a.x2 <= b.x2 && b.y1 <= a.y1 && a.y1 < b.y2) { return true; } if (a.x1 < b.x2 && b.x2 <= a.x2 && a.y1 < b.y2 && b.y2 <= a.y2) { return true; } if (a.x1 <= b.x1 && b.x1 < a.x2 && a.y1 <= b.y1 && b.y1 < a.y2) { return true; } if (b.x1 <= a.x1 && a.x1 < b.x2 && b.y1 <= a.y1 && a.y1 < b.y2) { return true; } if (a.x1 <= b.x1 && b.x1 < a.x2 && a.y1 < b.y2 && b.y2 <= a.y2) { return true; } if (a.x1 <= b.x1 && a.x2 >= b.x2 && b.y1 <= a.y1 && b.y2 >= a.y2) { return true; } if (b.x1 <= a.x1 && b.x2 >= a.x2 && a.y1 <= b.y1 && a.y2 >= b.y2) { return true; } if (a.x1 < b.x2 && b.x2 <= a.x2 && a.y1 < b.y2 && b.y2 <= a.y2) { return true; } if (a.x1 < b.x2 && b.x2 <= a.x2 && a.y1 <= b.y1 && b.y1 < a.y2) { return true; } return false; } void dodajDoWyniku(struct prost& a) { bool dodano = true; for (int i = 0; i < wynik.size() && dodano; i++) { if (czyPrzeciecie(wynik[i], a)) { struct prost nowy; nowy.x1 = min(a.x1, wynik[i].x1); nowy.x2 = max(a.x2, wynik[i].x2); nowy.y1 = min(a.y1, wynik[i].y1); nowy.y2 = max(a.y2, wynik[i].y2); wynik.erase(wynik.begin() + i); dodajDoWyniku(nowy); dodano = false; } } if (dodano) wynik.push_back(a); } int main() { int n; scanf("%d", &n); int x1, x2, y1, y2; for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &x1, &x2, &y1, &y2); struct prost nowyProstokat; nowyProstokat.x1 = x1; nowyProstokat.x2 = x2; nowyProstokat.y1 = y1; nowyProstokat.y2 = y2; dodajDoWyniku(nowyProstokat); } sort(wynik.begin(), wynik.end(), cmp1); printf("%d\n", wynik.size()); for (int i = 0; i < wynik.size(); i++) { printf("%d %d %d %d\n", wynik[i].x1, wynik[i].x2, wynik[i].y1, wynik[i].y2); } return 0; } |