• L


    Problem F: Fabled Rooks

    ACM: <wbr>uva <wbr>11134We 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 (xliyli) and its right-lower corner (xriyri), 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 xliylixri, andyri. 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 outputn 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 


     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 //#include <cmath>   命名冲突   y1 
     5 #include <algorithm>
     6 #include <string>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <list>
    13 #include <iomanip>
    14 #include <cstdlib>
    15 #include <sstream>
    16 using namespace std;
    17 typedef long long LL;
    18 const int INF=0x5fffffff;
    19 const double EXP=1e-8;
    20 const int MS=5005;
    21 
    22 int x1[MS], y1[MS], x2[MS], y2[MS], x[MS], y[MS];
    23 
    24 /*
    25 先将各个车分配在同一列的不同行,然后分配不同的列,
    26 使他们彼此错开,任意两个车不在同一列和同一行。
    27 也就是说行和列的分配时可以分开的。或者说独立的
    28 使用贪心法分配。
    29 */
    30 
    31 bool solve(int *a,int *b,int *c,int n)
    32 {
    33    // memset(c,-1,sizeof(c));    注意这样是错误的,因为不知道c到哪里结束。字符串指针才可以,因为有结束符 
    34    fill(c,c+n,-1);          //  ==-1表示还没有分配
    35     for(int col=1;col<=n;col++)
    36     {
    37         int rook=-1,minb=n+1;
    38         for(int i=0;i<n;i++)
    39         {
    40             if(c[i]<0&&col>=a[i]&&b[i]<minb)
    41             {
    42                 rook=i;
    43                 minb=b[i];
    44             }
    45         }
    46         if(rook<0||col>minb)
    47             return false;
    48         c[rook]=col;
    49     }
    50     return true;
    51 }
    52 
    53 int main()
    54 {
    55     int n;
    56     while(scanf("%d",&n)&&n)
    57     {
    58         for(int i=0;i<n;i++)
    59             scanf("%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i]);
    60         if(solve(x1,x2,x,n)&&solve(y1,y2,y,n))
    61             for(int i=0;i<n;i++)
    62                 printf("%d %d
    ",x[i],y[i]);
    63         else
    64             printf("IMPOSSIBLE
    ");
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    批量给对象属性赋值
    判断linq语句结果是否为空的方法
    linq var出来的object取值问题
    LinQ转换运算符OfType<T>
    C#退出的几种方式
    return的用法
    扩展文字
    Timer的使用方法
    通过16道练习学习Linq和Lambda
    VS2010 无可用源
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4335970.html
Copyright © 2020-2023  润新知