Uva11134
#include<bits/stdc++.h> #define inf 0x3f3f3f3f const int maxn=5000; using namespace std; int n; struct rook{ int x1,y1; int x2,y2; int id; int resx,resy; int size(){ return abs(x1 - x2) * abs(x1 - x2) + abs(y1 - y2) * abs(y1 - y2); } int width(){ return abs(x1 - x2); } int len(){ return abs(y1 - y2); } }rooks[maxn+10]; int flagx[maxn+10]; int flagy[maxn+10]; bool cmpx(rook a, rook b){ /*if(a.width() == b.width()){ return a.x1 < b.x1; } return a.width() < b.width();*/ //刚开始想错了,这么去贪心,是错的。 /* 反例 * 1111 * 1 * 1 * 1 * 11 */ if(a.x2 == b.x2){ return a.x1 < b.x1; } return a.x2 < b.x2; } bool cmpy(rook a, rook b){ /*if(a.len() == b.len()){ return a.y1 < b.y1; } return a.len() < b.len();*/ if(a.y2 == b.y2){ return a.y1 < b.y1; } return a.y2 < b.y2; } bool cmp1(rook a, rook b){ return a.id < b.id; } int main() { while(scanf("%d",&n)!=EOF&&n){ int res = 0; memset(flagx, 0, sizeof(flagx)); memset(flagy, 0, sizeof(flagy)); for(int i = 0; i < n; ++i){ scanf("%d%d%d%d",&rooks[i].x1,&rooks[i].y1,&rooks[i].x2,&rooks[i].y2); rooks[i].id = i; } sort(rooks, rooks + n, cmpx); for(int i = 0; i < n; ++i){ int flag = 0; for(int j = rooks[i].x1; j <= rooks[i].x2; ++j){ if(!flagx[j]){ flagx[j] = 1; rooks[i].resx = j; flag = 1; break; } } if(!flag){ res = 1; break; } } if(!res){ sort(rooks, rooks + n, cmpy); for(int i = 0; i < n; ++i){ int flag = 0; for(int j = rooks[i].y1; j <= rooks[i].y2; ++j){ if(!flagy[j]){ flagy[j] = 1; rooks[i].resy = j; flag = 1; break; } } if(!flag){ res = 1; break; } } } if(res){ printf("IMPOSSIBLE\n"); } else { sort(rooks, rooks + n, cmp1); for(int i = 0; i < n; ++i){ printf("%d %d\n",rooks[i].resx,rooks[i].resy); } } } return 0; }