UVa 11134 - Fabled Rooks——[问题分解、贪心法]
2016-04-13 01:24
316 查看
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 rightlower 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, andyri.
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 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 Sample Output 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
解题思路:
经过分析可以发现,每个车横纵坐标的选取是相互独立的,因此可以分别考虑。那么问题可以简化成:将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=5000; struct Rec{ int xl,yl,xr,yr; }; Rec rect[maxn+5]; int id[maxn+5]; int id_row_num[maxn+5]; int id_col_num[maxn+5]; int row[maxn+5]; int col[maxn+5]; int n; void init(){ memset(id_row_num, 0, sizeof id_row_num); memset(id_col_num, 0, sizeof id_col_num); memset(row, 0, sizeof row); memset(col,0,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=1;i<=n;i++) id[i]=i; sort(id+1, id+1+n, cmp_row); //bool ok=true; for(int i=1;i<=n;i++){ Rec &t=rect[id[i]]; bool flag=false; for(int j=t.xl;j<=t.xr;j++){ if(row[j]==0){ row[j]=1; id_row_num[id[i]]=j; flag=true; break; } } if(!flag){ return false; } } return true; } bool solve_col(){ for(int i=1;i<=n;i++) id[i]=i; sort(id+1, id+1+n, cmp_col); //bool ok=true; for(int i=1;i<=n;i++){ Rec &t=rect[id[i]]; bool flag=false; for(int j=t.yl;j<=t.yr;j++){ if(col[j]==0){ col[j]=1; 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)==1&&n){ init(); for(int i=1;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=1;i<=n;i++) printf("%d %d\n",id_row_num[i],id_col_num[i]); } else printf("IMPOSSIBLE\n"); //time__; } //time__; return 0; }
相关文章推荐
- 软件工程师怎样减轻工作过程中遇到的压力
- Android View分区域点击实现方案——1.根据坐标范围
- 求杨辉三角的第n行
- DICOM-RT:放疗领域中的各种影像
- hao-单例模式(Singleton)
- 创业的第一百六十四天
- ubuntu 环境 celery配置全解
- [LeetCode]27. Remove Element
- Unity Shaders and Effects Cookbook (3-5) 金属软高光
- leetcode 25 Reverse Nodes in k-Group
- 安装完Android Studio出现Internal error. Please report to https://code.google.com/p/android/issues
- hdu 2546 饭卡
- 用栈实现(+-*/)运算
- TextField的简单使用
- flash Timer类使用
- vim 插件之supertab
- Android SDK Manager 更新代理配置
- Elasticsearch Restore失灵
- 初识C++之虚函数
- ServletContextListener的用法