UVA - 11134 Fabled Rooks[贪心 问题分解]
| UVA - 11134 |
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 rectan- gle 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.
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, and
yri. The input file is terminated with the integer ‘0’ 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
8
1122
5788
2255
2255
6386
6385
6388
3678
8
1122
5788
2255
2255
6386
6385
6388
3678
0Sample Output
11
58
24
42
73
85
66
37
11
58
24
42
73
85
66
37
白书
在n*n的棋盘上面放n个车,能否使他们互相不攻击(即不能在同一行一列),并且第i个车必须落在第i的矩形范围(xl,yl, xr,yr)之内
//
// main.cpp
// uva11134
//
// Created by Candy on 10/17/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=5e3+,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n;
int x[N],y[N];
struct data{
int id;
int l1,r1,l2,r2;
}a[N];
//bool cmp1(data &x,data &y){
// if(x.l1==y.l1) return x.r1<y.r1;
// else return x.l1<y.l1;
//}
//bool cmp2(data &x,data &y){
// if(x.l2==y.l2) return x.r2<y.r2;
// return x.l2<y.l2;
//}
//bool solve(){
// sort(a+1,a+1+n,cmp1);
// int p=1;
// for(int i=1;i<=n;i++){
// //printf("hi%d %d %d %d\n",i,a[i].l1,a[i].r1,a[i].id);
// if(a[p].l1<=i&&i<=a[p].r1) x[a[p++].id]=i;
// else if(a[p].r1<i||a[p].l1>i) return 0;
// }
//
// sort(a+1,a+1+n,cmp2);
// p=1;
// for(int i=1;i<=n;i++){
// //printf("ih%d %d %d %d\n",i,a[i].l2,a[i].r2,a[i].id);
// if(a[p].l2<=i&&i<=a[p].r2) y[a[p++].id]=i;
// else if(a[p].r2<i||a[p].l2>i) return 0;
// }
// return 1;
//}
bool sol(){
memset(x,,sizeof(x));
memset(y,,sizeof(y));
for(int i=;i<=n;i++){
int rook=,mnr=INF;
for(int j=;j<=n;j++)
if(!x[j]&&a[j].l1<=i&&a[j].r1<mnr) rook=j,mnr=a[j].r1;
//printf("rook1 %d\n",rook);
if(rook==||a[rook].r1<i) return false;
x[rook]=i;
}
for(int i=;i<=n;i++){
int rook=,mnr=INF;
for(int j=;j<=n;j++)
if(!y[j]&&a[j].l2<=i&&a[j].r2<mnr) rook=j,mnr=a[j].r2;
//printf("rook2 %d\n",rook);
if(rook==||a[rook].r2<i) return false;
y[rook]=i;
}
return ;
}
int main(int argc, const char * argv[]) {
while((n=read())){
for(int i=;i<=n;i++){
a[i].id=i;
a[i].l1=read();a[i].l2=read();a[i].r1=read();a[i].r2=read();
}
if(sol()){
for(int i=;i<=n;i++) printf("%d %d\n",x[i],y[i]);
}else printf("IMPOSSIBLE\n");
} return ;
}
UVA - 11134 Fabled Rooks[贪心 问题分解]的更多相关文章
- UVA 11134 Fabled Rooks 贪心
题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...
- 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——[问题分解、贪心法]
We would like to place n rooks, ≤ n ≤ , on a n × n board subject to the following restrictions • The ...
- uva 11134 - Fabled Rooks(问题转换+优先队列)
题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...
- 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 优先队列,贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...
随机推荐
- 修改nginx版本名称伪装任意web server
如何修改nginx默认的名称,可以稍微的伪装一下,也可以装x 一般来说修改3个位置,一个是nginx.h.另一个是ngx_http_header_filter_module.c.还有一个ngx_htt ...
- python基础之文件处理
读和写文件 读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直 ...
- 03-树1 树的同构 (C语言链表实现)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...
- svg.js教程及使用手册详解(二)
上篇简要介绍了svg.js的基本信息和基本用法,这篇开始详细讲解svg.js的用法. SVG元素 SVG元素主要包括各种形状.线条.文本.路径. 矩形——Rect Rects有两个参数,即矩形的宽度和 ...
- Cropper – 简单的 jQuery 图片裁剪插件
Cropper 是一个简单的 jQuery 图像裁剪插件.它支持选项,方法,事件,触摸(移动),缩放,旋转.输出的裁剪数据基于原始图像大小,这样你就可以用它们来直接裁剪图像. 如果你尝试裁剪跨域图像, ...
- jQuery实现右上角点击后滑下来的竖向菜单
效果体验请点击这里:http://keleyi.com/a/bjad/27095rgj.htm 这个菜单我觉得可以做成在线客服之类的,点击下滑后,带关闭按钮,因此在不想显示的时候可以关掉它. 以下是源 ...
- 一次部署HTTPS的相关事件引发的思考
前言: 上周五快要下班的时候,突然收到通知客户希望了解一下部署HTTPS的流程,这种事情谁听了都会有几分诧异的.因为这件事虽然和工作有一定的相关度,但平时不会走这个方向,实际上也较少接触.此外,客户手 ...
- Autodesk正在招聘Civil、Infraworks金牌支持工程师(Premium Support Specialist)
Civil Infraworks金牌支持工程师,也不知道中文这么翻对不对,反正很牛的,地点优选上海,不过其他地区也没问题啊,感兴趣的,赶紧扔简历过来,我当你内线,帮你内推 :) Autodesk是全球 ...
- Linux0.11内核--内存管理之2.配合fork
[版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5598451.html ] 在上一篇的fork函数中,首先一上来就调用get_free_page ...
- React Native知识2-Text组件
Text用于显示文本的React组件,并且它也支持嵌套.样式,以及触摸处理.在下面的例子里,嵌套的标题和正文文字会继承来自styles.baseText的fontFamily字体样式,不过标题上还附加 ...