题意:在n*n的棋盘上放n个车,使得任意两个车不相互攻击,且第i个车在一个给定的矩形Ri之内,不相互攻击是指不同行不同列,无解输出IMPOSSIBLE,否则分别输出第1,2,……,n个车的坐标。
分析:行和列是无关的,因此把原题分解成两个一维问题。在区间[1,n]内选择n个不同的整数,使得第i个整数在闭区间[n1i, n2i]内。按r优先排序。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-8; const int MAXN = 5000 + 10; const int MAXT = 10000 + 10; using namespace std; bool vis[MAXN]; int ans[MAXN][3]; int n; struct Node{ int l, r, id; Node(){} bool operator < (const Node& a)const{ return r < a.r; } }num1[MAXN], num2[MAXN]; bool judge(Node *num, int x){ memset(vis, false, sizeof vis); sort(num + 1, num + n + 1); for(int i = 1; i <= n; ++i){ bool ok = false; for(int j = num[i].l; j <= num[i].r; ++j){ if(!vis[j]){ vis[j] = true; ans[num[i].id][x] = j; ok = true; break; } } if(!ok) return false; } return true; } int main(){ while(scanf("%d", &n) == 1){ if(!n) return 0; memset(ans, 0, sizeof ans); for(int i = 1; i <= n; ++i){ scanf("%d%d%d%d", &num1[i].l, &num2[i].l, &num1[i].r, &num2[i].r); num1[i].id = num2[i].id = i; } if(!judge(num1, 0)){ printf("IMPOSSIBLE\n"); continue; } if(!judge(num2, 1)){ printf("IMPOSSIBLE\n"); continue; } for(int i = 1; i <= n; ++i){ printf("%d %d\n", ans[i][0], ans[i][1]); } } return 0; }