#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; struct plemie { int x1; int x2; int y1; int y2; }; vector <plemie> tab; vector <plemie> tab2; bool tab3[100000]; bool jest(int a, int b) { if((tab[a].x1 < tab[b].x2) && (tab[a].x2 > tab[b].x1) && (tab[a].y2 > tab[b].y1) && (tab[a].y1 < tab[b].y2)) return true; if((tab[b].x1 < tab[a].x2) && (tab[b].x2 > tab[a].x1) && (tab[b].y2 > tab[a].y1) && (tab[b].y1 < tab[a].y2)) return true; return false; } bool sort2(const plemie &a, const plemie &b) { if(a.x1 < b.x1) return true; else if(a.x1 == b.x1) { if(a.x2 < b.x2) return true; else if(a.x2 == b.x2) { if(a.y1 < b.y1) return true; else if(a.y1 == b.y1) { if(a.y2 < b.y2) return true; return false; } return false; } return false; } return false; } int main() { int n, x1, x2, y2, y1, a; plemie tmp; bool czy = true; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d %d %d %d", &x1, &x2, &y1, &y2); tmp.x1 = x1; tmp.x2 = x2; tmp.y1 = y1; tmp.y2 = y2; tab.push_back(tmp); } while(czy) { czy = false; a = tab.size(); for(int i = 0; i < a; i++) { for(int j = i + 1; j < a; j++) { if(jest(i, j)) { //cout<<i<<" "<<j<<endl; tmp.x1 = min(tab[i].x1, tab[j].x1); tmp.x2 = max(tab[i].x2, tab[j].x2); tmp.y1 = min(tab[i].y1, tab[j].y1); tmp.y2 = max(tab[i].y2, tab[j].y2); tab2.push_back(tmp); czy = true; tab3[i] = true; tab3[j] = true; } } } // cout<<tab2.size()<<" "<<tab.size()<<endl; for(int i = 0; i < a; i++) { if(tab3[i] == false) { tab2.push_back(tab[i]); } tab3[i] = false; } tab.clear(); tab = tab2; tab2.clear(); //cout<<tab2.size()<<" "<<tab.size()<<endl; } printf("%d\n", tab.size()); sort(tab.begin(), tab.end(), sort2); for(int i = 0; i < tab.size(); i++) { printf("%d %d %d %d\n", tab[i].x1, tab[i].x2, tab[i].y1, tab[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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; struct plemie { int x1; int x2; int y1; int y2; }; vector <plemie> tab; vector <plemie> tab2; bool tab3[100000]; bool jest(int a, int b) { if((tab[a].x1 < tab[b].x2) && (tab[a].x2 > tab[b].x1) && (tab[a].y2 > tab[b].y1) && (tab[a].y1 < tab[b].y2)) return true; if((tab[b].x1 < tab[a].x2) && (tab[b].x2 > tab[a].x1) && (tab[b].y2 > tab[a].y1) && (tab[b].y1 < tab[a].y2)) return true; return false; } bool sort2(const plemie &a, const plemie &b) { if(a.x1 < b.x1) return true; else if(a.x1 == b.x1) { if(a.x2 < b.x2) return true; else if(a.x2 == b.x2) { if(a.y1 < b.y1) return true; else if(a.y1 == b.y1) { if(a.y2 < b.y2) return true; return false; } return false; } return false; } return false; } int main() { int n, x1, x2, y2, y1, a; plemie tmp; bool czy = true; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d %d %d %d", &x1, &x2, &y1, &y2); tmp.x1 = x1; tmp.x2 = x2; tmp.y1 = y1; tmp.y2 = y2; tab.push_back(tmp); } while(czy) { czy = false; a = tab.size(); for(int i = 0; i < a; i++) { for(int j = i + 1; j < a; j++) { if(jest(i, j)) { //cout<<i<<" "<<j<<endl; tmp.x1 = min(tab[i].x1, tab[j].x1); tmp.x2 = max(tab[i].x2, tab[j].x2); tmp.y1 = min(tab[i].y1, tab[j].y1); tmp.y2 = max(tab[i].y2, tab[j].y2); tab2.push_back(tmp); czy = true; tab3[i] = true; tab3[j] = true; } } } // cout<<tab2.size()<<" "<<tab.size()<<endl; for(int i = 0; i < a; i++) { if(tab3[i] == false) { tab2.push_back(tab[i]); } tab3[i] = false; } tab.clear(); tab = tab2; tab2.clear(); //cout<<tab2.size()<<" "<<tab.size()<<endl; } printf("%d\n", tab.size()); sort(tab.begin(), tab.end(), sort2); for(int i = 0; i < tab.size(); i++) { printf("%d %d %d %d\n", tab[i].x1, tab[i].x2, tab[i].y1, tab[i].y2); } return 0; } |