【高效算法设计——问题分解】Uva11134 Fabled Rooks
来源:互联网 发布:跟京东学什么java技术 编辑:程序博客网 时间:2024/06/14 21:12
Fabled Rooks
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
Description
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 (xli, yli) and its right-lower corner (xri, yri), 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 xli, yli, xri, 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
81 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 881 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 80
Output for sample input
1 15 82 44 27 38 56 63 71 15 82 44 27 38 56 63 7
题意:给定n*n的矩形框,在上面摆放n个车,使得任意两个车都不在同一列或同一行,并且给定了每个车的放置范围,求是否存在解
思路:首先可以将题意转化为在n*n的方格内,每一行和每一列都只放一个车,可以注意到,本题行和列是不相关冲突的,可以将这个二维问题分解为两个一维问题考虑,在行方向上,可以转化为给定n个区间,将1~n之间的数一一分配给n个区间。可以使用贪心法,将每个区间按右端点最小排序,然后一一枚举1~n得出解,即为1~n选择区间的时候每次选满足要求且右边最小的区间
代码如下
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=5000+5;typedef struct{ int l,r,u,d,id,x,y,visx,visy;}Node;Node x[maxn];bool cmp1(const Node &t1,const Node &t2){ if(t1.r==t2.r) return t1.l<t2.l; else return t1.r<t2.r;}bool cmp2(const Node &t1,const Node &t2){ if(t1.d==t2.d) return t1.u<t2.u; else return t1.d<t2.d;}bool cmp3(const Node &t1,const Node &t2){ return t1.id<t2.id;}int main(){ int i,j,k,n,ans; bool flag; while(scanf("%d",&n)==1 && n) { flag=true; for(i=1;i<=n;i++) { scanf("%d%d%d%d",&x[i].l,&x[i].u,&x[i].r,&x[i].d); x[i].id=i; x[i].visx=x[i].visy=0; } sort(x+1,x+1+n,cmp1); for(i=1;i<=n;i++) { bool ok=false; for(j=1;j<=n;j++) { if(x[j].visx) continue; if(x[j].l<=i && x[j].r>=i) { x[j].visx=1; x[j].x=i; ok=true; break; } } if(!ok) { flag=false; break; } } if(!flag) puts("IMPOSSIBLE"); else { sort(x+1,x+1+n,cmp2); for(i=1;i<=n;i++) { bool ok=false; for(j=1;j<=n;j++) { if(x[j].visy) continue; if(x[j].u<=i && x[j].d>=i) { x[j].visy=1; x[j].y=i; ok=true; break; } } if(!ok) { flag=false; break; } } if(!flag) puts("IMPOSSIBLE"); else { sort(x+1,x+1+n,cmp3); for(i=1;i<=n;i++) printf("%d %d\n",x[i].x,x[i].y); } } } return 0;}
0 0
- 【高效算法设计——问题分解】Uva11134 Fabled Rooks
- uva11134 fabled rooks 贪心+问题独立分解
- Uva11134 Fabled Rooks【问题分解+贪心】【例题8-4】
- UVA11134- Fabled Rooks
- UVa11134 - Fabled Rooks(贪心)
- uva11134 -Fabled Rooks
- UVA11134 Fabled Rooks
- UVA11134 Fabled Rooks(贪心)
- UVA11134-Fabled Rooks(贪心)
- Uva11134 Fabled Rooks(贪心,优先级队列)
- 解题报告 之 UVA11134 Fabled Rooks
- Uva 11134 Fabled Rooks (问题分解 + 贪心放置)
- uva 11134——Fabled Rooks
- uva 11134 fabled rooks (贪心)——yhx
- uva 11134 fabled rooks (贪心)——yhx
- UVa 11134 Fabled Rooks ——区间选点
- Fabled Rooks
- Fabled Rooks
- Linux菜鸟笔记
- 如何在已经发布站点的IIS上,将已发布的低版本的ASP.NET升级成高版本
- Spec下载方法汇总
- 保存网络上的图片到本地计算机
- poj3977 Subset(折半枚举)
- 【高效算法设计——问题分解】Uva11134 Fabled Rooks
- Computing Recommendations at Extreme Scale with Apache Flink and Google Compute Engine
- Cocos2d-x——读取Csv配置文件
- Ubuntu12.04 用iptables作NAT代理上网
- 黑马程序员--继承、抽象、接口(Java)
- win_iocp_io_service
- leetcode: two sum
- linux 读取设备节点值
- 安保法制の与党協議―立ち止まって考えること