UVa 11134 - Fabled Rooks——[问题分解、贪心法]
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——[问题分解、贪心法]的更多相关文章
- 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 ...
- uva 11134 - Fabled Rooks(问题转换+优先队列)
题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...
- UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...
- UVA 11134 Fabled Rooks 贪心
题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...
- UVa 11134 Fabled Rooks (贪心+问题分解)
题意:在一个n*n的棋盘上放n个车,让它们不互相攻击,并且第i辆车在给定的小矩形内. 析:说实话,一看这个题真是没思路,后来看了分析,原来这个列和行是没有任何关系的,我们可以分开看, 把它变成两个一维 ...
- UVA - 11134 Fabled Rooks问题分解,贪心
题目:点击打开题目链接 思路:为了满足所有的车不能相互攻击,就要保证所有的车不同行不同列,于是可以发现,行与列是无关的,因此题目可以拆解为两个一维问题,即在区间[1-n]之间选择n个不同的整数,使得第 ...
- 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 ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- UVA 11134 Fabled Rooks
贪心+优先队列+问题分解 对x,y 分开处理 当 xl<cnt(当前处理行)时,不能简单的选择cnt,而是应该让xl=cnt 并重新加入优先队列.(y的处理同上) #include <io ...
随机推荐
- concurrent模块
concurrent包 concurrent.futrues模块 3.2版本引入 异步并行任务模块,提供一个高级的异步可执行的便利接口. 提供了两个池执行器 ThreadPoolExecutor异步调 ...
- 仿Google Nexus菜单样式
在线演示 本地下载
- SDUT-3373_数据结构实验之查找一:二叉排序树
数据结构实验之查找一:二叉排序树 Time Limit: 400 ms Memory Limit: 65536 KiB Problem Description 对应给定的一个序列可以唯一确定一棵二叉排 ...
- 阿里云DDoS高防的演进:防御效果成核心
分布式拒绝服务(DDoS)攻击这一网络公敌,是任何互联网业务的重大威胁.随着DDoS攻击工具化的发展,无论是简单野蛮的流量型攻击,还是复杂精巧的应用型攻击,黑客发起DDoS攻击变得越来越简单和自动化. ...
- thinkphp常用的一些函数
$this->display ( "Public:login" ); import ( 'Wechat', APP_PATH . 'Common/Wechat', '.cla ...
- Hdu 4810
2014-05-02 15:53:50 题目连接 2013年南京现场赛的题目,现场的时候,排在我们前面的队伍基本都过了这题,我们后面的队伍也有不少过了这题,唯独我们没有.. 后来是Qingyu Sha ...
- 人生苦短,LET'S GO! GO语言目录
1.Golang开山篇,GO就是NB! 1-1.go开发工具安装 2.go-人生第一个go程序和基本语法 3.go-流程控制 4.go-函数 5.go-流程控制 6.go-复合类型 7.go-面向对象 ...
- nginx设置301永久重定向
https://blog.csdn.net/wzqzhq/article/details/53376501 比如说我的域名有多个,一个主域名www.zq110.com,多个次域名:www.aaa.co ...
- Win10家庭版如何启用本地组策略
组策略对于优化和维护Windows系统来说十分重要.众所周知,Windows 10家庭版中并不包含组策略,对于使用家庭版Windows的朋友来说,十分不方便.小编将以Windows10家庭版为例,带大 ...
- Mysql 查询一天中每半小时记录的数量
SELECT HOUR(e.time)as Hour,FLOOR(MINUTE(e.time)/30) as M, COUNT(*) as Count FROM error_log e WHERE e ...