zoukankan      html  css  js  c++  java
  • 贪心 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个有放置范围的车的一种方案,要求车不能相互攻击。
      这题有一个很巧妙的性质:行和列是不相关联的,考虑处理两次,贪心即可。
      
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 using namespace std;
     6 const int maxn=5010;
     7 
     8 int L[maxn],R[maxn],U[maxn],D[maxn],P[maxn];
     9 int X[maxn],Y[maxn];
    10 bool cmp1(int a,int b){
    11     if(R[a]!=R[b])return R[a]<R[b];
    12     return L[a]<L[b];
    13 }
    14 
    15 bool cmp2(int a,int b){
    16     if(D[a]!=D[b])return D[a]<D[b];
    17     return U[a]<U[b];
    18 }
    19 bool vis[maxn];
    20 int main(){
    21     int n,cnt;
    22     while(~scanf("%d",&n)&&n){
    23         for(int i=1;i<=n;i++)
    24             scanf("%d%d%d%d",&U[i],&L[i],&D[i],&R[i]);
    25         for(int i=1;i<=n;i++)P[i]=i;
    26         bool OK=true,flag;
    27         sort(P+1,P+n+1,cmp1);
    28         memset(vis,0,sizeof(vis));cnt=1;
    29         for(int i=1,j;i<=n;i++){
    30             while(vis[P[cnt]])cnt++;flag=false;
    31             for(j=cnt;j<=n;j++)
    32                 if(!vis[P[j]]&&i<=R[P[j]]&&i>=L[P[j]]){
    33                     flag=true;
    34                     break;
    35                 }        
    36             OK&=flag;
    37             if(!OK)break;
    38             Y[P[j]]=i;
    39             vis[P[j]]=true;
    40         }
    41         sort(P+1,P+n+1,cmp2);
    42         memset(vis,0,sizeof(vis));cnt=1;
    43         for(int i=1,j;i<=n;i++){
    44             while(vis[P[cnt]])cnt++;flag=false;
    45             for(j=cnt;j<=n;j++)
    46                 if(!vis[P[j]]&&i<=D[P[j]]&&i>=U[P[j]]){
    47                     flag=true;
    48                     break;
    49                 }        
    50             OK&=flag;
    51             if(!OK)break;
    52             X[P[j]]=i;
    53             vis[P[j]]=true;
    54         }
    55         if(OK){
    56             for(int i=1;i<=n;i++)
    57                 printf("%d %d
    ",X[i],Y[i]);
    58             }
    59         else
    60             printf("IMPOSSIBLE
    ");
    61     }
    62     return 0;
    63 }

    尽最大的努力,做最好的自己!
  • 相关阅读:
    【Android】Android连接SQLite3数据库的操作
    【exe4j】如何利用exe4j把java桌面程序生成exe文件
    Http网络协议
    【Spring】spring的7个模块
    【jsp】JSP中page指令isThreadSafe
    【MySQL】乐观锁和悲观锁
    【Eclipse】Eclipse上如何集成SVN插件
    【MySQL】mysql出现错误“ Every derived table must have its own alias”
    【Struts2】SSH如何返回JSON数据
    【Oracle】Oracle 的过程化SQL(PLSQL)中NULL值的处理
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5303694.html
Copyright © 2011-2022 走看看