【高效算法设计——问题分解】Uva11134 Fabled Rooks

来源:互联网 发布:跟京东学什么java技术 编辑:程序博客网 时间:2024/06/14 21:12
Fabled Rooks
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

Description

Download as PDF

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 (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, 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

81 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 881 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 80

Output for sample input

1 15 82 44 27 38 56 63 71 15 82 44 27 38 56 6

3 7


题意:给定n*n的矩形框,在上面摆放n个车,使得任意两个车都不在同一列或同一行,并且给定了每个车的放置范围,求是否存在解

思路:首先可以将题意转化为在n*n的方格内,每一行和每一列都只放一个车,可以注意到,本题行和列是不相关冲突的,可以将这个二维问题分解为两个一维问题考虑,在行方向上,可以转化为给定n个区间,将1~n之间的数一一分配给n个区间。可以使用贪心法,将每个区间按右端点最小排序,然后一一枚举1~n得出解,即为1~n选择区间的时候每次选满足要求且右边最小的区间

代码如下
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=5000+5;typedef struct{    int l,r,u,d,id,x,y,visx,visy;}Node;Node x[maxn];bool cmp1(const Node &t1,const Node &t2){    if(t1.r==t2.r)        return t1.l<t2.l;    else        return t1.r<t2.r;}bool cmp2(const Node &t1,const Node &t2){    if(t1.d==t2.d)        return t1.u<t2.u;    else        return t1.d<t2.d;}bool cmp3(const Node &t1,const Node &t2){        return t1.id<t2.id;}int main(){    int i,j,k,n,ans;    bool flag;    while(scanf("%d",&n)==1 && n)    {        flag=true;        for(i=1;i<=n;i++)        {            scanf("%d%d%d%d",&x[i].l,&x[i].u,&x[i].r,&x[i].d);            x[i].id=i;            x[i].visx=x[i].visy=0;        }        sort(x+1,x+1+n,cmp1);        for(i=1;i<=n;i++)        {            bool ok=false;            for(j=1;j<=n;j++)            {                if(x[j].visx)                    continue;                if(x[j].l<=i && x[j].r>=i)                {                    x[j].visx=1;                    x[j].x=i;                    ok=true;                    break;                }            }            if(!ok)            {                flag=false;                break;            }        }        if(!flag)            puts("IMPOSSIBLE");        else        {            sort(x+1,x+1+n,cmp2);        for(i=1;i<=n;i++)        {            bool ok=false;            for(j=1;j<=n;j++)            {                if(x[j].visy)                    continue;                if(x[j].u<=i && x[j].d>=i)                {                    x[j].visy=1;                    x[j].y=i;                    ok=true;                    break;                }            }            if(!ok)            {                flag=false;                break;            }        }        if(!flag)            puts("IMPOSSIBLE");        else        {            sort(x+1,x+1+n,cmp3);            for(i=1;i<=n;i++)                printf("%d %d\n",x[i].x,x[i].y);        }        }    }    return 0;}



0 0
原创粉丝点击