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. Leetcode690.Employee Importance员工的重要性

    给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度为15, 10, 5.那么员工1的数据结构是[1 ...

  2. thinkphp---display与fetch区别

    区别: ① display方法直接输出模板文件渲染后的内容,fetch方法是返回模板文件渲染后的内容 ② 有时候我们不想直接输出模板内容,而是希望对内容再进行一些处理后输出, 就可以使用fetch方法 ...

  3. concurrent模块

    concurrent包 concurrent.futrues模块 3.2版本引入 异步并行任务模块,提供一个高级的异步可执行的便利接口. 提供了两个池执行器 ThreadPoolExecutor异步调 ...

  4. .net 数据表格显示控件

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/chenjinge7/article/details/30470609 1. GridView 控件 ...

  5. 如何手动解析CrashLog

    http://www.cocoachina.com/ios/20150803/12806.html 解决崩溃问题是移动应用开发者最日常的工作之一.如果是开发过程中遇到的崩溃,可以根据重现步骤调试,但线 ...

  6. Person Re-identification 系列论文笔记(二):A Discriminatively Learned CNN Embedding for Person Re-identification

    A Discriminatively Learned CNN Embedding for Person Re-identification Zheng Z, Zheng L, Yang Y. A Di ...

  7. 【NS2】NS2在ubuntu下的安装

    Step1: 更新系统.在终端输入如下命令 sudo apt-get  update #更新源列表sudo apt-get upgrade #更新已经安装的包sudo apt-get dist-upg ...

  8. oracle获取中文出现乱码问题解决

    首先搞清楚字符集和字符编码概念,了解oracle字符集原理,请参考一位大神的讲解: ref:http://blog.csdn.net/dbanote/article/details/9158367#c ...

  9. 前端基础☞CSS

    css的四种引入方式 1.行内式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出CSS的优势,不推荐使用. <p style="background-color: ...

  10. oracle函数 RAWTOHEX(c1)

    [功能]将一个二进制构成的字符串转换为十六进制 [参数]c1,二进制的字符串 [返回]字符串 [示例] select RAWTOHEX('A123')  from dual;