#include <stdio.h>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<pair<int,int>,pair<int,int>> prostokat;
#define X1 first.first
#define Y1 first.second
#define X2 second.first
#define Y2 second.second
int n;
prostokat wczytaj()
{
prostokat a;
scanf("%d%d%d%d",&a.X1,&a.X2,&a.Y1,&a.Y2);
return a;
}
prostokat nowy(int x1,int x2,int y1,int y2)
{
prostokat a;
a.X1 = x1;
a.X2=x2;
a.Y1 = y1;
a.Y2=y2;
return a;
}
multiset<prostokat> z;
void wypisz(prostokat a);
bool przerwax(prostokat a, prostokat b)
{
return b.X1 >= a.X2 || b.X2<=a.X1;
}
bool przerway(prostokat a, prostokat b)
{
return b.Y1 >= a.Y2 || b.Y2<=a.Y1;
}
bool przecina(prostokat a, prostokat b)
{
// printf("przecina\n");
// printf(" ");wypisz(a);
// printf(" ");wypisz(b);
bool r = !(przerwax(a,b) || przerway(a,b));
// printf(" %s\n",r?"TAK":"NIE");
return r;
}
void wypisz(prostokat a)
{
printf("%d %d %d %d\n",a.X1,a.X2,a.Y1,a.Y2);
}
void wypiszz()
{
for(multiset<prostokat>::iterator it = z.begin();it!=z.end();++it)
{
wypisz(*it);
}
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
prostokat a = wczytaj();
z.insert(a);
}
bool flag = false;
while (!flag)
{
flag = true;
bool find = false;
for(multiset<prostokat>::iterator it = z.begin();it!=z.end()&&!find;++it)
{
for(multiset<prostokat>::iterator it2=it;it2!=z.end()&&!find;++it2)
{
if(it!=it2 && przecina(*it,*it2))
{
find = true;
flag = false;
prostokat a = *it;
prostokat b = *it2;
// wypiszz();
z.erase(a);
z.erase(b);
// wypiszz();
z.insert(nowy(min(a.X1,b.X1),max(a.X2,b.X2),min(a.Y1,b.Y1),max(a.Y2,b.Y2)));
// wypiszz();
}
}
}
}
printf("%d\n",z.size());
wypiszz();
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 | #include <stdio.h> #include <set> #include <algorithm> #include <iostream> using namespace std; typedef pair<pair<int,int>,pair<int,int>> prostokat; #define X1 first.first #define Y1 first.second #define X2 second.first #define Y2 second.second int n; prostokat wczytaj() { prostokat a; scanf("%d%d%d%d",&a.X1,&a.X2,&a.Y1,&a.Y2); return a; } prostokat nowy(int x1,int x2,int y1,int y2) { prostokat a; a.X1 = x1; a.X2=x2; a.Y1 = y1; a.Y2=y2; return a; } multiset<prostokat> z; void wypisz(prostokat a); bool przerwax(prostokat a, prostokat b) { return b.X1 >= a.X2 || b.X2<=a.X1; } bool przerway(prostokat a, prostokat b) { return b.Y1 >= a.Y2 || b.Y2<=a.Y1; } bool przecina(prostokat a, prostokat b) { // printf("przecina\n"); // printf(" ");wypisz(a); // printf(" ");wypisz(b); bool r = !(przerwax(a,b) || przerway(a,b)); // printf(" %s\n",r?"TAK":"NIE"); return r; } void wypisz(prostokat a) { printf("%d %d %d %d\n",a.X1,a.X2,a.Y1,a.Y2); } void wypiszz() { for(multiset<prostokat>::iterator it = z.begin();it!=z.end();++it) { wypisz(*it); } } int main() { scanf("%d",&n); for(int i=0;i<n;i++) { prostokat a = wczytaj(); z.insert(a); } bool flag = false; while (!flag) { flag = true; bool find = false; for(multiset<prostokat>::iterator it = z.begin();it!=z.end()&&!find;++it) { for(multiset<prostokat>::iterator it2=it;it2!=z.end()&&!find;++it2) { if(it!=it2 && przecina(*it,*it2)) { find = true; flag = false; prostokat a = *it; prostokat b = *it2; // wypiszz(); z.erase(a); z.erase(b); // wypiszz(); z.insert(nowy(min(a.X1,b.X1),max(a.X2,b.X2),min(a.Y1,b.Y1),max(a.Y2,b.Y2))); // wypiszz(); } } } } printf("%d\n",z.size()); wypiszz(); return 0; } |
English