Fabled Rooks UVA - 11134

版权声明:欢迎转载!拒绝抄袭. https://blog.csdn.net/qq_36257146/article/details/89848033
#include <iostream>
#include <bits/stdc++.h>
#define maxn 5000+10
using namespace std;


bool solve(int * a,int * b,int n,int * c)
{
    fill(c+1,c+n+1,-1);
    for(int i = 1;i<=n;i++)//每一列
    {
        int id = -1,minb = n+1;
        for(int j = 1;j<=n;j++)//Çø¼ä
        {
            if( b[j]>=i &&b[j]<minb && i>=a[j] && c[j]<0)
            {
                minb = b[j];
                id = j;
            }
        }
        if(id == -1 ) return false;
        c[id] = i;
    }
    return true;
}


int main()
{
    int n;
    int x1[maxn],x2[maxn],y1[maxn],y2[maxn];
    int c1[maxn],c2[maxn];
    while(cin>>n&&n)
    {
        for(int i = 1; i<=n; i++)
        {
            cin>>x1[i]>>y1[i]>>x2[i]>>y2[i];
        }
        if(solve(x1,x2,n,c1) && solve(y1,y2,n,c2))
        {

            for(int i = 1;i<=n;i++)
            {
                cout<<c1[i]<<" "<<c2[i]<<endl;
            }
        }
        else{
            cout<<"IMPOSSIBLE"<<endl;
        }
    }
    return 0;
}
/**
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
**/

猜你喜欢

转载自blog.csdn.net/qq_36257146/article/details/89848033