11134 Fabled Rooks
阿新 • • 發佈:2019-02-05
#include<cstdio> #include<iostream> #include<cstring> #include<string.h> #include<algorithm> using namespace std; const int maxn = 5000 + 5; struct node { int x1, x2, y1, y2; int id; }k[maxn]; int x[maxn]; int y[maxn]; bool cmp1(node p, node q) { if(p.x2==q.x2) return p.x1 < q.x1; return p.x2 < q.x2; } bool cmp2(node p, node q) { if(p.y2==q.y2) return p.y1 < q.y1; return p.y2 < q.y2; } int main() { int n; while(scanf("%d", &n) ==1 && n) { memset(k,0,sizeof(k)); memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); for(int i = 0; i < n; ++i) { scanf("%d%d%d%d", &k[i].x1, &k[i].y1, &k[i].x2, &k[i].y2); k[i].id = i;} int cnt[maxn]; bool ok = false; memset(cnt,0,sizeof(cnt)); sort(k,k+n, cmp1); for(int i = 0; i < n;++i) { ok = false; for(int j = k[i].x1; j <= k[i].x2; ++j) { if(!cnt[j]) { ok = true; x[k[i].id] = j; cnt[j] = 1; break; } } if(!ok) break; } if(!ok) { cout << "IMPOSSIBLE" << endl; continue; } memset(cnt,0,sizeof(cnt)); sort(k,k+n, cmp2); for(int i = 0; i < n;++i) { ok = false; for(int j = k[i].y1; j <= k[i].y2; ++j) { if(!cnt[j]) { ok = true; y[k[i].id] = j; cnt[j] = 1; break; } } if(!ok) break; } if(!ok) { cout << "IMPOSSIBLE" << endl; continue; } for(int i = 0; i < n; ++i) printf("%d %d\n", x[i], y[i]); } return 0; }