uva 11134 Fabled Rooks

来源:互联网 发布:淘宝晒图返现违规吗 编辑:程序博客网 时间:2024/06/07 20:33

原题:
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 (xl i ,yl i ) and its right-lower corner (xr i ,yr i ), where 1 ≤ i ≤ n, 1 ≤ xl i ≤xr i ≤ n, 1 ≤ yl i ≤ yr i ≤ 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 xl i , yl i , xr i , and yr i . 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

中文:
给你一个n*n的棋盘,然后给你n组数,表示n个矩形框。每组数里面4个点,分别表示矩形框的左上角和右下角。现在问你第i个点放在第i个矩形框中,且放置点的行和列没有其他点。问你放置的方法。

#include <bits/stdc++.h>using namespace std;struct point{    int x,y,i;};struct node{    int x,i;};int n;vector<point> vpx,vpy;vector<node> ansx,ansy;int cmp(const point &lhs,const point &rhs){    if(lhs.y!=rhs.y)        return lhs.y<rhs.y;    else        return lhs.x<rhs.x;}int ncmp(const node &lhs,const node &rhs){    return lhs.i<rhs.i;}unordered_set<int> usi;int main(){    ios::sync_with_stdio(false);    int flag;    while(cin>>n)    {        if(n==0)            break;        flag=1;        vpx.clear();        vpy.clear();        ansx.clear();        ansy.clear();        for(int i=0;i<n;i++)        {            int xl,yl,xr,yr;            cin>>xl>>yl>>xr>>yr;            vpx.push_back(point{xl,xr,i});            vpy.push_back(point{yl,yr,i});        }        sort(vpx.begin(),vpx.end(),cmp);        sort(vpy.begin(),vpy.end(),cmp);        usi.clear();        for(int i=0;i<vpx.size();i++)        {            int mark=0;            for(int j=vpx[i].x;j<=vpx[i].y;j++)            {                if(usi.find(j)==usi.end())                {                    ansx.push_back(node{j,vpx[i].i});                    usi.insert(j);                    mark=1;                    break;                }            }            if(!mark)            {                flag=0;                break;            }        }        if(flag)        {            usi.clear();            for(int i=0;i<vpy.size();i++)            {                int mark=0;                for(int j=vpy[i].x;j<=vpy[i].y;j++)                {                    if(usi.find(j)==usi.end())                    {                        ansy.push_back(node{j,vpy[i].i});                        usi.insert(j);                        mark=1;                        break;                    }                }                if(!mark)                {                    flag=0;                    break;                }            }        }        if(flag==0)        {            cout<<"IMPOSSIBLE"<<endl;        }        else        {            sort(ansx.begin(),ansx.end(),ncmp);            sort(ansy.begin(),ansy.end(),ncmp);            for(int i=0;i<ansx.size();i++)            {                cout<<ansx[i].x<<" "<<ansy[i].x<<endl;            }        }    }    return 0;}

思路:
紫书上的例题
由于横纵之间没有关系,所以可以单独的把矩形区间分解成x区间和y区间来判断,然后使用贪心法,将区间按照右端点值从小到大排序,放置点即可。
(不能按照左端点排序,例如[1,1] [2,2] [1,3]这三个点)

0 0
原创粉丝点击