#include <cstdio> #include <algorithm> #include <list> #include <vector> //#define debug(x...) fprintf(stderr, x) #define debug(x...) struct CRectangle { int x1, y1, x2, y2; CRectangle* next; CRectangle* prev; }; //std::list<CRectangle> countries; CRectangle *listRoot; std::vector<CRectangle*> sortedList; bool compareRectangles(CRectangle *first, CRectangle *second) { if (first->x1 == second->x1) { if (first->x2 == second->x2) { if (first->y1 == second->y1) { return (first->y2 < second->y2); } else return (first->y1 < second->y1); } else return (first->x2 < second->x2); } else return (first->x1 < second->x1); } bool isPointInsideRectangle(const int x, const int y, const CRectangle* rect) { if (x > rect->x1 && x < rect->x2 && y > rect->y1 && y < rect->y2) { return true; } return false; } bool rectangles_cross2(const CRectangle* first, const CRectangle* second) { debug("Comparing %d %d %d %d with %d %d %d %d\n", first->x1, first->x2, first->y1, first->y2, second->x1, second->x2, second->y1, second->y2); if (second->x2 > first->x1 && first->x2 > second->x1 && second->y2 > first->y1 && first->y2 > second->y1) { debug("giving true\n"); return true; } debug("giving false\n"); return false; } bool rectangles_cross(const CRectangle* first, const CRectangle* second) { debug("Comparing %d %d %d %d with %d %d %d %d\n", first->x1, first->x2, first->y1, first->y2, second->x1, second->x2, second->y1, second->y2); if (isPointInsideRectangle(first->x1, first->y1, second) || isPointInsideRectangle(first->x1, first->y2, second) || isPointInsideRectangle(first->x2, first->y1, second) || isPointInsideRectangle(first->x2, first->y2, second) || isPointInsideRectangle(second->x1, second->y1, first) || isPointInsideRectangle(second->x1, second->y2, first) || isPointInsideRectangle(second->x2, second->y1, first) || isPointInsideRectangle(second->x2, second->y2, first) ) { debug("giving true\n"); return true; } debug("giving false\n"); return false; } void rectangles_merge(CRectangle& mergeTo, const CRectangle& merged) { mergeTo.x1 = std::min(mergeTo.x1, merged.x1); mergeTo.x2 = std::max(mergeTo.x2, merged.x2); mergeTo.y1 = std::min(mergeTo.y1, merged.y1); mergeTo.y2 = std::max(mergeTo.y2, merged.y2); } CRectangle* checkWithAll(CRectangle* num) { for (CRectangle *i = listRoot; i != NULL; i = i->next) { if (num == i) continue; if (rectangles_cross2(i, num)) { return i; } } return NULL; } int main() { int n; scanf("%d", &n); int countries = n; for (int i = 0; i < n; ++i) { CRectangle *tmp = new CRectangle; scanf("%d %d %d %d", &tmp->x1, &tmp->x2, &tmp->y1, &tmp->y2); if (tmp->x1 > tmp->x2) std::swap(tmp->x1, tmp->x2); if (tmp->y1 > tmp->y2) std::swap(tmp->y1, tmp->y2); tmp->prev = NULL; tmp->next = listRoot; listRoot = tmp; CRectangle* cur = listRoot; while (true) { CRectangle* found = checkWithAll(cur); if (NULL != found) { rectangles_merge(*found, *cur); countries--; if (cur->prev != NULL) (cur->prev)->next = cur->next; else listRoot = cur->next; if (cur->next != NULL) (cur->next)->prev = cur->prev; delete cur; cur = found; } else break; } } printf("%d\n", countries); //sort for (CRectangle* i = listRoot; i != NULL; i = i->next) { sortedList.push_back(i); } std::sort(sortedList.begin(), sortedList.end(), compareRectangles); for (std::vector<CRectangle*>::iterator it = sortedList.begin(); it != sortedList.end(); ++it) { printf("%d %d %d %d\n", (*it)->x1, (*it)->x2, (*it)->y1, (*it)->y2); } }
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 | #include <cstdio> #include <algorithm> #include <list> #include <vector> //#define debug(x...) fprintf(stderr, x) #define debug(x...) struct CRectangle { int x1, y1, x2, y2; CRectangle* next; CRectangle* prev; }; //std::list<CRectangle> countries; CRectangle *listRoot; std::vector<CRectangle*> sortedList; bool compareRectangles(CRectangle *first, CRectangle *second) { if (first->x1 == second->x1) { if (first->x2 == second->x2) { if (first->y1 == second->y1) { return (first->y2 < second->y2); } else return (first->y1 < second->y1); } else return (first->x2 < second->x2); } else return (first->x1 < second->x1); } bool isPointInsideRectangle(const int x, const int y, const CRectangle* rect) { if (x > rect->x1 && x < rect->x2 && y > rect->y1 && y < rect->y2) { return true; } return false; } bool rectangles_cross2(const CRectangle* first, const CRectangle* second) { debug("Comparing %d %d %d %d with %d %d %d %d\n", first->x1, first->x2, first->y1, first->y2, second->x1, second->x2, second->y1, second->y2); if (second->x2 > first->x1 && first->x2 > second->x1 && second->y2 > first->y1 && first->y2 > second->y1) { debug("giving true\n"); return true; } debug("giving false\n"); return false; } bool rectangles_cross(const CRectangle* first, const CRectangle* second) { debug("Comparing %d %d %d %d with %d %d %d %d\n", first->x1, first->x2, first->y1, first->y2, second->x1, second->x2, second->y1, second->y2); if (isPointInsideRectangle(first->x1, first->y1, second) || isPointInsideRectangle(first->x1, first->y2, second) || isPointInsideRectangle(first->x2, first->y1, second) || isPointInsideRectangle(first->x2, first->y2, second) || isPointInsideRectangle(second->x1, second->y1, first) || isPointInsideRectangle(second->x1, second->y2, first) || isPointInsideRectangle(second->x2, second->y1, first) || isPointInsideRectangle(second->x2, second->y2, first) ) { debug("giving true\n"); return true; } debug("giving false\n"); return false; } void rectangles_merge(CRectangle& mergeTo, const CRectangle& merged) { mergeTo.x1 = std::min(mergeTo.x1, merged.x1); mergeTo.x2 = std::max(mergeTo.x2, merged.x2); mergeTo.y1 = std::min(mergeTo.y1, merged.y1); mergeTo.y2 = std::max(mergeTo.y2, merged.y2); } CRectangle* checkWithAll(CRectangle* num) { for (CRectangle *i = listRoot; i != NULL; i = i->next) { if (num == i) continue; if (rectangles_cross2(i, num)) { return i; } } return NULL; } int main() { int n; scanf("%d", &n); int countries = n; for (int i = 0; i < n; ++i) { CRectangle *tmp = new CRectangle; scanf("%d %d %d %d", &tmp->x1, &tmp->x2, &tmp->y1, &tmp->y2); if (tmp->x1 > tmp->x2) std::swap(tmp->x1, tmp->x2); if (tmp->y1 > tmp->y2) std::swap(tmp->y1, tmp->y2); tmp->prev = NULL; tmp->next = listRoot; listRoot = tmp; CRectangle* cur = listRoot; while (true) { CRectangle* found = checkWithAll(cur); if (NULL != found) { rectangles_merge(*found, *cur); countries--; if (cur->prev != NULL) (cur->prev)->next = cur->next; else listRoot = cur->next; if (cur->next != NULL) (cur->next)->prev = cur->prev; delete cur; cur = found; } else break; } } printf("%d\n", countries); //sort for (CRectangle* i = listRoot; i != NULL; i = i->next) { sortedList.push_back(i); } std::sort(sortedList.begin(), sortedList.end(), compareRectangles); for (std::vector<CRectangle*>::iterator it = sortedList.begin(); it != sortedList.end(); ++it) { printf("%d %d %d %d\n", (*it)->x1, (*it)->x2, (*it)->y1, (*it)->y2); } } |