UVa 11134 Fabled Rooks

Problem F: 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 (xliyli) and its right-lower corner (xriyri), 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.

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 xliylixri, and yri. The input file is terminated with the integer `0' on a line by itself.

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

Output for sample input

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

K. Diks, adapted by P. Rudnicki


#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
// 区间的结构体
typedef struct s_node
{
	int left, right;	// 区间的左右端点
	int num;		// 区间代表的皇后编号
	bool operator < (const struct s_node& x) const
	{
		return (right < x.right) || (right == x.right && left < x.left);
	}				
}s_node;

// 代表行,列的区间
s_node row_array[5010];
s_node col_array[5010];
// 棋盘大小
int n;

// 皇后位置
int queen_row[5010];
int queen_col[5010];

// 行,列使用情况
int row_flag[5010];
int col_flag[5010];

int main()
{
	while(scanf("%d", &n) == 1 && (n != 0))
	{
		memset(row_flag, 0, sizeof(row_flag));
		memset(col_flag, 0, sizeof(col_flag));
		// 读入行和列的各个区间
		for(int i = 0; i < n; i++)
		{
			int x1, y1, x2, y2;
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			row_array[i].left = x1;
			row_array[i].right = x2;
			row_array[i].num = i;
			col_array[i].left = y1;
                        col_array[i].right = y2;
                        col_array[i].num = i;	
		}

		// 按区间的端点(先左后右)从小到大排序
		sort(row_array, row_array+n);
		sort(col_array, col_array+n);
/*		printf("row:\n");
		for(int i = 0; i < n; i++)
			printf("[%d,%d]\n", row_array[i].left, row_array[i].right);
		printf("col:\n");
                for(int i = 0; i < n; i++)
                        printf("[%d,%d]\n", col_array[i].left, col_array[i].right);
*/		int i;
		// 查看区间i是否能包含一个整数,如果不能,就无法放置皇后,否则可以放置
		for(i = 0; i < n; i++)
		{
			/*
			if(!(row_array[i].left <= i+1 && row_array[i].right >= i+1))
				break;
			if(!(col_array[i].left <= i+1 && col_array[i].right >= i+1))
				break;
			queen_row[row_array[i].num] = i+1;
			queen_col[col_array[i].num] = i+1;		*/
			int j;
			for(j = row_array[i].left; j <= row_array[i].right; j++)
			{
				if(row_flag[j] == 0)
				{
					row_flag[j] = 1;
					queen_row[row_array[i].num] = j;
					break;
				}
			}	
			if(j == row_array[i].right+1)
				break;
			for(j = col_array[i].left; j <= col_array[i].right; j++)
                        {
                                if(col_flag[j] == 0)
                                {
                                        col_flag[j] = 1;
                                        queen_col[col_array[i].num] = j;
                                        break;
                                }
                        }
                        if(j == col_array[i].right+1)
                                break;	
		}		 
		if(i < n)
			printf("IMPOSSIBLE\n");
		else
		{
			for(int i = 0; i < n; i++)
				printf("%d %d\n", queen_row[i], queen_col[i]);	
		}
	}
	return 0;	
}

这道题一开始没想出来。后来看了书中提示,将行和列分别考虑。

在n个区间中选n个不同的点,所有点属于[1,n]. 

一开始想是根据左端点来排序,后来发现应该以右端点进行排序。然后总是选择最左边可行的点。

证明思路:

设相邻两个区间为

(a1, b1)

(a2, b2).

假设(a1,b1)中选择的点是x.

1.如果按照右端点排序,则b2>b1 或者(b2=b1,a2>=a1)

如果x < a2那么没有对(a2,b2)选择点造成任何影响。

如果x >= a2. 由于(a1,b1)余下可以选择的点,(a2,b2)也可以选,不会有影响。

2.如果按照左端点排序,那么如果x >= a2. (a1,b1)余下可以选择的点,(a2,b2)未必可以选。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值