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. (续)使用Django搭建一个完整的项目(Centos7+Nginx)

    django-admin startproject web cd web 2.配置数据库(使用Mysql) vim web/settings.py #找到以下并按照实际情况修改 DATABASES = ...

  2. 海胜专访--MaxCompute 与大数据查询引擎的技术和故事

    摘要:在2019大数据技术公开课第一季<技术人生专访>中,阿里巴巴云计算平台高级技术专家苑海胜为大家分享了<MaxCompute 与大数据查询引擎的技术和故事>,主要介绍了Ma ...

  3. Python的bisect模块

    Python的列表(list)类型内部是一个线性表,在线性表中查找元素复杂度为O(N),即调用list.index()的复杂的是O(N).当数据量较大时,应该使用二分查找优化,二分查找范围每次缩小一般 ...

  4. 【Leetcode栈】有效的括号(20)

    题目 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 1,左括号必须用相同类型的右括号闭合. 2,左括号必须以正确的顺序闭合. 注意 ...

  5. Android开发-API指南-<activity-alias>[原创译文]

    http://blog.sina.com.cn/s/blog_48d491300100zmg5.html

  6. framework7日期插件使用

    1.引入框架文件 <link rel="stylesheet" href="framework7.ios.min.css"> <link re ...

  7. Libevent:11使用Libevent的DNS上层和底层功能

    Libevent提供了一些API用来进行DNS域名解析,并且提供了实现简单DNS服务器的能力. 本章首先描述域名解析的上层功能,然后介绍底层功能及服务器功能. 注意:Libevent的当前DNS客户端 ...

  8. 2018-7-5-dotnet-设计规范-·-抽象定义

    title author date CreateTime categories dotnet 设计规范 · 抽象定义 lindexi 2018-07-05 15:48:20 +0800 2018-2- ...

  9. keep-alive vue组件缓存避免多次加载相应的组件

    keep-alive vue组件缓存避免多次加载相应的组件

  10. Python中json和eval的区别

    >>> import json >>> s = '{"one":1,"two":2}' >>> json. ...