We would like to place n rooks,  ≤ n ≤ , 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 ≤ i ≤ n, ≤ xli ≤xri ≤ n, ≤ 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, andyri.
The input file is terminated with the integer ‘’ 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

Sample Output

解题思路:

  经过分析可以发现,每个车横纵坐标的选取是相互独立的,因此可以分别考虑。那么问题可以简化成:将1~n 分别放入[xli,xri]n个区间中。选择的方法采用贪心法——以行为例,先将各个矩形区间按 xr 从小到大排序 [xl1,xr1][xl2,xr2]...[xln,xrn];依次选择每个区间中尽量靠xl的未放置车的位置,比如针对[xl1,xr1],应将车放入xl1位置,此时对于区间[xl2,xr2],xl1位置不再考虑。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
using namespace std;
#define time__ printf("time: %f\n",double(clock())/CLOCKS_PER_SEC)
const int maxn=;
struct Rec{
int xl,yl,xr,yr;
};
Rec rect[maxn+];
int id[maxn+];
int id_row_num[maxn+];
int id_col_num[maxn+];
int row[maxn+];
int col[maxn+];
int n;
void init(){
memset(id_row_num, , sizeof id_row_num);
memset(id_col_num, , sizeof id_col_num);
memset(row, , sizeof row);
memset(col,,sizeof col);
}
bool cmp_row(int &a,int &b){
return rect[a].xr<rect[b].xr;
}
bool cmp_col(int &a,int &b){
return rect[a].yr<rect[b].yr;
}
bool solve_row(){
for(int i=;i<=n;i++)
id[i]=i;
sort(id+, id++n, cmp_row);
//bool ok=true;
for(int i=;i<=n;i++){
Rec &t=rect[id[i]];
bool flag=false;
for(int j=t.xl;j<=t.xr;j++){
if(row[j]==){
row[j]=;
id_row_num[id[i]]=j;
flag=true;
break;
}
}
if(!flag){
return false;
}
}
return true;
}
bool solve_col(){
for(int i=;i<=n;i++)
id[i]=i;
sort(id+, id++n, cmp_col);
//bool ok=true;
for(int i=;i<=n;i++){
Rec &t=rect[id[i]];
bool flag=false;
for(int j=t.yl;j<=t.yr;j++){
if(col[j]==){
col[j]=;
id_col_num[id[i]]=j;
flag=true;
break;
}
}
if(!flag){
return false;
}
}
return true;
} int main(int argc, const char * argv[]) {
while(scanf("%d",&n)==&&n){
init();
for(int i=;i<=n;i++)
scanf("%d%d%d%d",&rect[i].xl,&rect[i].yl,&rect[i].xr,&rect[i].yr);
if(solve_row()&&solve_col()){
for(int i=;i<=n;i++)
printf("%d %d\n",id_row_num[i],id_col_num[i]);
}
else printf("IMPOSSIBLE\n");
//time__;
}
//time__;
return ;
}

UVa 11134 - Fabled Rooks——[问题分解、贪心法]的更多相关文章

  1. UVA - 11134 Fabled Rooks[贪心 问题分解]

    UVA - 11134 Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to t ...

  2. uva 11134 - Fabled Rooks(问题转换+优先队列)

    题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...

  3. UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)

    题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...

  4. UVA 11134 Fabled Rooks 贪心

    题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...

  5. UVa 11134 Fabled Rooks (贪心+问题分解)

    题意:在一个n*n的棋盘上放n个车,让它们不互相攻击,并且第i辆车在给定的小矩形内. 析:说实话,一看这个题真是没思路,后来看了分析,原来这个列和行是没有任何关系的,我们可以分开看, 把它变成两个一维 ...

  6. UVA - 11134 Fabled Rooks问题分解,贪心

    题目:点击打开题目链接 思路:为了满足所有的车不能相互攻击,就要保证所有的车不同行不同列,于是可以发现,行与列是无关的,因此题目可以拆解为两个一维问题,即在区间[1-n]之间选择n个不同的整数,使得第 ...

  7. uva 11134 fabled rooks (贪心)——yhx

    We would like to place n rooks, 1 n 5000, on a n nboard subject to the following restrictions• The i ...

  8. UVA 11134 - Fabled Rooks(贪心+优先队列)

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

  9. UVA 11134 Fabled Rooks

    贪心+优先队列+问题分解 对x,y 分开处理 当 xl<cnt(当前处理行)时,不能简单的选择cnt,而是应该让xl=cnt 并重新加入优先队列.(y的处理同上) #include <io ...

随机推荐

  1. Leetcode868.Binary Gap二进制间距

    给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的二进制是 0b10110 . ...

  2. 廖雪峰Python总结2

    1.切片 L[0:3]表示,从索引0开始,直到索引3为止,但是不包括索引3.如果第一个索引是0,还可以省略L[:3] 倒数切片:L[-n:-1],-1是倒数第一个元素,L[-n:-1]不包括倒数第一个 ...

  3. Spring_Bean的作用域---和使用外部属性文件

    <!-- 使用 bean的scope属性来配置bean的作用域 singleton:默认值.容器初始时创建bean实例,在整个容器的生命周期内只创建这一个bean单例 prototype:原型的 ...

  4. Python接口自动化(二)接口开发

    django 配置开发环境 相关命令 python manage.py runserver 127.0.0.1:8000在指定的地址和端口启动服务 python manage.py startapp ...

  5. SPSS统计基础-均值功能的使用

    SPSS统计基础-均值功能的使用 均值过程计算一个或多个自变量类别中因变量的子组均值和相关的单变量统计.您也可以获得单因素方差分析.eta 和线性相关检验. 统计量.合计.个案数.均值.中位数.组内中 ...

  6. mongoDB端口启动失败原因

    删除以下文件: (所以数据会丢失,需要重新创建数据库)

  7. SDUT-3361_迷宫探索

    数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 有一个地下迷宫,它的通道都是直的,而通道 ...

  8. int 和bigint差别有多大?

    https://bbs.csdn.net/wap/topics/230059600 请问在mysql中int和bigint差别有多大?在什么情况下需要用到bigint? bigint 带符号的范围是- ...

  9. SDUT-1479_数据结构实验之栈与队列九:行编辑器

    数据结构实验之栈与队列九:行编辑器 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个简单的行编辑程序的功能是:接受用 ...

  10. 使用css制作三角

    1. 字符实现三角效果关于字符实现三角我早在09年的时候就介绍了:使用字符实现兼容性的圆角尖角效果.一转眼两年过去了,这个技术开始被越来越多的人所熟知.使用的字符是正棱形“◆”字符,编码表示为◆ . ...