UVA 11134

本文介绍了如何通过贪心算法解决一个特定的棋盘布局问题,即在一个n*n的棋盘上放置n个车,同时满足每个车的特定摆放限制。文章详细解释了算法步骤,包括按右边界排序区间、寻找空位放置车以及判断布局可行性。通过实例演示,读者可以理解并应用这种解决问题的方法。
摘要由CSDN通过智能技术生成

寒假训练七:E题

传送门:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=107154#problem/E

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

• The i-th rook can only be placed within the rectanglegiven by its left-upper corner (xli, yli) and its rightlowercorner (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 rookscan occupy the same column or the same row.

Input

The input consists of several test cases. The first line of eachof them contains one integer number, n, the side of the board. n lines follow giving the rectangleswhere the rooks can be placed as described above. The i-th line among them gives xli, yli, xri, andyri. 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 nlines each giving the position of a rook in order in which their rectangles appeared in the input. If thereare 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


题意:给一个n*n的棋盘往里面放n个车,每行每列只能有一个车

每个车都有一个限定范围,要求第i辆车不能超过第i行限定的矩形


贪心,首先行和列互相没有影响可以转化成两个线性问题

同时将区间按右边界排序

从第一个线段开始找到第一个没有点的地方放入

要是有区间没办法放点了就impossible


之前欠的太多了,还不如先写刚做的...

盯了这题好久以为是搜索,结果贪心就好了

没有认真看完题目, 一开始impossible忘了写

听完方法确实有道理,然而就是想不到...

还要多练啊

下面代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<functional>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;

struct node{
	int q,z,i,a,mark;
}x[50005],y[50005];

bool cmp(node a,node b)
{
	if(a.z==b.z) return a.q<b.q;
	return a.z<b.z;
}

bool cmp1(node a,node b)
{
	return a.i<b.i;
}


int main()
{
	int i,j,n,dmark,ddmark;
	int mark[10005];
	
	while(~scanf("%d",&n))
	{
		memset(mark,0,sizeof(mark));
		if(n==0) break;
		ddmark=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d%d%d%d",&x[i].q,&y[i].q,&x[i].z,&y[i].z);
			x[i].i=y[i].i=i;
		}
		sort(x+1,x+n+1,cmp);
		sort(y+1,y+n+1,cmp);
		for(i=1;i<=n;i++)
		{
			dmark=0;
			for(j=x[i].q;j<=x[i].z;j++)
			{
				if(mark[j]==0) break;
			}
			if(j>x[i].z) dmark=1;
			x[i].a=j;
			mark[j]=1;
			if(dmark==1) 
			{
				ddmark=1;
				printf("IMPOSSIBLE\n");
				break;
			}
		}
		memset(mark,0,sizeof(mark));
		for(i=1;i<=n;i++)
		{
			dmark=0;	
			for(j=y[i].q;j<=y[i].z;j++)
			{
				if(mark[j]==0) break;
			}
			if(j>y[i].z) dmark=1;
			y[i].a=j;
			mark[j]=1;
			if(dmark==1) 
			{
				ddmark=1;
				printf("IMPOSSIBLE\n");
				break;
			}
		}
		if(ddmark==0)
		{
			sort(x+1,x+n+1,cmp1);
			sort(y+1,y+n+1,cmp1);
			for(i=1;i<=n;i++)
				printf("%d %d\n",x[i].a,y[i].a);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值