题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65530#problem/D
解题思路:由于行和列之间是不相互影响的,因此我们对行和列分开进行处理。然后对于每行和每列采用贪心的方法进行安置即可。
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 5010;
bool mark[maxn];
struct Node {
int x1, y1, x2, y2, id;
Node() { }
Node(int _x1, int _y1, int _x2, int _y2, int _id) : x1(_x1), y1(_y1), x2(_x2), y2(_y2), id(_id) { }
}nd[maxn];
bool comp1(const Node &p1, const Node &p2) {
if(p1.x2 == p2.x2) {
return p1.x1 < p2.x1;
}
return p1.x2 < p2.x2;
}
bool comp2(const Node &p1, const Node &p2) {
if(p1.y2 == p2.y2) {
return p1.y1 < p2.y1;
}
return p1.y2 < p2.y2;
}
int x[maxn], y[maxn];
int main() {
freopen("aa.in", "r", stdin);
int n;
bool flag;
while(scanf("%d", &n) == 1) {
if(n == 0) break;
flag = true;
for(int i = 0; i < n; ++i) {
nd[i].id = i;
scanf("%d %d %d %d", &nd[i].x1, &nd[i].y1, &nd[i].x2, &nd[i].y2);
}
memset(mark, false, sizeof(mark));
sort(nd, nd + n, comp1);
bool ok;
for(int i = 0; i < n; ++i) {
ok = false;
for(int j = nd[i].x1; j <= nd[i].x2; ++j) {
if(!mark[j]) {
x[nd[i].id] = j;
mark[j] = true;
ok = true;
break;
}
}
if(!ok) {
flag = false;
break;
}
}
if(flag) {
sort(nd, nd + n, comp2);
memset(mark, false, sizeof(mark));
for(int i = 0; i < n; ++i) {
ok = false;
for(int j = nd[i].y1; j <= nd[i].y2; ++j) {
if(!mark[j]) {
y[nd[i].id] = j;
mark[j] = true;
ok = true;
break;
}
}
if(!ok) {
flag = false;
break;
}
}
}
if(flag) {
for(int i = 0; i < n; ++i) {
printf("%d %d\n", x[i], y[i]);
}
} else {
printf("IMPOSSIBLE\n");
}
}
return 0;
}