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

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

 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

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个车,使得任意两个车都不在同一列或同一行,并且给定了每个车的放置范围,求是否存在解

思路:首先可以将题意转化为在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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值