贪心 uvaoj 11134 Fabled Rooks
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
8
1 1 2 2
5 7 8 8
2 2 5 5
2 2 5 5
6 3 8 6
6 3 8 5
6 3 8 8
3 6 7 8
8
1 1 2 2
5 7 8 8
2 2 5 5
2 2 5 5
6 3 8 6
6 3 8 5
6 3 8 8
3 6 7 8
0
Output for sample input
1 1
5 8
2 4
4 2
7 3
8 5
6 6
3 7
1 1
5 8
2 4
4 2
7 3
8 5
6 6
3 7
对于一个N*N的棋盘,求放置N个有放置范围的车的一种方案,要求车不能相互攻击。
这题有一个很巧妙的性质:行和列是不相关联的,考虑处理两次,贪心即可。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=; int L[maxn],R[maxn],U[maxn],D[maxn],P[maxn];
int X[maxn],Y[maxn];
bool cmp1(int a,int b){
if(R[a]!=R[b])return R[a]<R[b];
return L[a]<L[b];
} bool cmp2(int a,int b){
if(D[a]!=D[b])return D[a]<D[b];
return U[a]<U[b];
}
bool vis[maxn];
int main(){
int n,cnt;
while(~scanf("%d",&n)&&n){
for(int i=;i<=n;i++)
scanf("%d%d%d%d",&U[i],&L[i],&D[i],&R[i]);
for(int i=;i<=n;i++)P[i]=i;
bool OK=true,flag;
sort(P+,P+n+,cmp1);
memset(vis,,sizeof(vis));cnt=;
for(int i=,j;i<=n;i++){
while(vis[P[cnt]])cnt++;flag=false;
for(j=cnt;j<=n;j++)
if(!vis[P[j]]&&i<=R[P[j]]&&i>=L[P[j]]){
flag=true;
break;
}
OK&=flag;
if(!OK)break;
Y[P[j]]=i;
vis[P[j]]=true;
}
sort(P+,P+n+,cmp2);
memset(vis,,sizeof(vis));cnt=;
for(int i=,j;i<=n;i++){
while(vis[P[cnt]])cnt++;flag=false;
for(j=cnt;j<=n;j++)
if(!vis[P[j]]&&i<=D[P[j]]&&i>=U[P[j]]){
flag=true;
break;
}
OK&=flag;
if(!OK)break;
X[P[j]]=i;
vis[P[j]]=true;
}
if(OK){
for(int i=;i<=n;i++)
printf("%d %d\n",X[i],Y[i]);
}
else
printf("IMPOSSIBLE\n");
}
return ;
}
贪心 uvaoj 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 (贪心)——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 优先队列,贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 11134 Fabled Rooks(贪心)
题目链接 题意 在n*n的棋盘上的n个指定区间上各放1个'车’ , 使他们相互不攻击(不在同行或同列),输出一种可能的方法. 分析 每行每列都必须放车,把行列分开看,若行和列同时有解,则问题有解. ...
- UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...
- UVa 11134 Fabled Rooks (贪心+问题分解)
题意:在一个n*n的棋盘上放n个车,让它们不互相攻击,并且第i辆车在给定的小矩形内. 析:说实话,一看这个题真是没思路,后来看了分析,原来这个列和行是没有任何关系的,我们可以分开看, 把它变成两个一维 ...
- UVA 11134 Fabled Rooks 贪心
题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...
随机推荐
- 发现并认为这是jQuery1.4.4的一个Bug
说起来还觉得丢人,公司的系统开发了两年,目前jquery的版本还是用的1.4.4. mantis上的Bug一堆,今天在改bug的时候发现一个jQuery的Bug. 改bug嘛,一开始总是各种调试,总感 ...
- js substr和substring字符串截取
substr(start,length)第一个参数是开始位置(注:start的开始是从0开始,看到好多博客上面是从1开始,在火狐和谷歌执行了一下是从0开始),第二个参数是截取字符串的长度(可以省略,表 ...
- SQLServer 触发器----增删改触发,两张表
ALTER TRIGGER [dbo].[PriceRange] ON [dbo].[Tab_SaleAndCarStyle] for update,insert,deleteASdecla ...
- 转载:修改xshell中文乱码的问题(管用)
执行echo $LANG命令输出的是当前的编码方式,执行locale命令得到系统中所有可用的编码方式.要让Xshell不显示乱码,则要将编码方式改为UTF-8. 在Xshell中[file]-> ...
- Adb工具常用操作-转(二)
一. PC与模拟器或真机交换文件(adb pull和adb push) 在开发阶段或其他原因,经常需要将PC上的文件复制到模拟器或真机上,或将模拟机和真机上的文件复制到PC上.使用adb pull和a ...
- windows 20003 扩展安装后不成功的原因
windows扩展如果安装不成功(PHP扩展)很大的可能就是那个DLL的权限不够.需要分配:AdministratorAuthenticater UsersIIS_WPGSYSTEMUsers
- 【转】 UITableViewCell的标记、移动、删除、插入
原文: http://blog.csdn.net/duxinfeng2010/article/details/7725897 这篇文章是建立在 代码实现 UITableView与UITableView ...
- Syntax error on token "package", assert expected------踩坑记录
今天写程序碰到个坑,eclipse编辑,jdk1.7,clean编译项目后报错Syntax error on token "package", assert expected 反复 ...
- 292. Nim Game(C++)
292. Nim Game(C++) You are playing the following Nim Game with your friend: There is a heap of stone ...
- cuda中时间用法
转载:http://blog.csdn.net/jdhanhua/article/details/4843653 在CUDA中统计运算时间,大致有三种方法: <1>使用cutil.h中的函 ...