UVA-11134 Fabled Rooks (贪心)

We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to the following restrictions

• The i-th rook can only be placed within the rectangle given by its left-upper corner (xli , yli) and its rightlower corner (xri , yri), where 1 ≤ i ≤ n, 1 ≤ xli ≤ xri ≤ n, 1 ≤ yli ≤ yri ≤ n.

• No two rooks can attack each other, that is no two rooks can occupy the same column or the same row.

Input

The input consists of several test cases. The first line of each of them contains one integer number, n, the side of the board. n lines follow giving the rectangles where the rooks can be placed as described above. The i-th line among them gives xli , yli , xri , and yri . The input file is terminated with the integer ‘0’ on a line by itself.

Output

Your task is to find such a placing of rooks that the above conditions are satisfied and then output n lines each giving the position of a rook in order in which their rectangles appeared in the input. If there are multiple solutions, any one will do. Output ‘IMPOSSIBLE’ if there is no such placing of the rooks.

Sample Input

8

1 1 2 2

5 7 8 8

2 2 5 5

2 2 5 5

6 3 8 6

6 3 8 5

6 3 8 8

3 6 7 8

8

1 1 2 2

5 7 8 8

2 2 5 5

2 2 5 5

6 3 8 6

6 3 8 5

6 3 8 8

3 6 7 8

0

Sample Output

1 1

5 8

2 4

4 2

7 3

8 5

6 6

3 7

1 1

5 8

2 4

4 2

7 3

8 5

6 6

3 7

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define  N 5010
using namespace std;
struct node {
	int xl,xr,yl,yr,x,y,id;
} a[N];
bool vis[N];
//只能按照ri右坐标从小到大排序,
//然后从左坐标开始选出最小的且未被占据的坐标。
bool cmpx(node x,node y) {
	if(x.xr==y.xr)
		return x.xl<y.xl;
	return x.xr<y.xr;
}
bool cmpy(node x,node y) {
	if(x.yr==y.yr)
		return x.yl<y.yl;
	return x.yr<y.yr;
}
bool cmp(node x,node y) {
	return x.id <y.id;
}
int main() {
	int n,k;
	while(~scanf("%d",&n)&&n) {
		for(int i=0; i<n; i++) {
			scanf("%d%d%d%d",&a[i].xl,&a[i].yl,&a[i].xr,&a[i].yr);
			a[i].x=a[i].y=0;
			a[i].id=i;
		}
		int flag=0;
		if(n>1) {
			sort(a,a+n,cmpx);
			memset(vis,false,sizeof(vis));
			for(int i=0; i<n; i++) {
				for(k=a[i].xl; k<=a[i].xr; k++) {
					if(vis[k]==false) {
						a[i].x=k;
						vis[k]=true;
						break;
					}
				}
				if(a[i].x==0) {
					flag=1;
					break;
				}
			}
			if(flag==0) {
				sort(a,a+n,cmpy);
				memset(vis,false,sizeof(vis));
				for(int i=0; i<n; i++) {
					for(k=a[i].yl; k<=a[i].yr; k++) {
						if(vis[k]==false) {
							a[i].y=k;
							vis[k]=true;
							break;
						}
					}
					if(a[i].y==0) {
						flag=1;
						break;
					}
				}
			}
		}
		if(n==1) {
			a[0].x=a[0].xl;
			a[0].y=a[0].yl;
		}
		if(flag==1)
			printf("IMPOSSIBLE\n");
		if(flag==0) {
			sort(a,a+n,cmp);
			for(int i=0; i<n; i++) {
				printf("%d %d\n",a[i].x,a[i].y );
			}
		}
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值