UVA 11134 Fabled Rooks

题目大意:给出n行n列的表,输入4个数字代表点的范围,范围是一个矩形,前俩个数字代表矩形左上角坐标,后俩个数字代表右下角坐标,要求取的点所在行和所在列与其他点不相交
解题思路:因为行和列不冲突,所以将每个点分为行和列单独讨论,将得到的每个点范围按最大值从小到达排列,然后贪心处理

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct node {
    int num, a, b; //a, b为x范围或者y范围,num是编号
};
bool cmp(node p, node q) {
    return p.b < q.b;
}
int n;
int rex[6000], rey[6000];
node nodex[6000];
node nodey[6000];
int sol() {
    for(int i = 1; i <= n; i++) {
        int k = 0;
        int j;
        for(j = 0; j <n ;j++) {
            int numb = nodex[j].num;
            if(rex[numb])
                continue;
            if(nodex[j].a <= i && nodex[j].b >= i) {
                rex[numb] = i;
                k = 1;
                break;
            }
        }
        if(!k)
            return 0;
        k = 0;
        for(j = 0; j <n ;j++) {
            int numb = nodey[j].num;                                            
            if(rey[numb])                                                                   
                continue;                                                                               
            if(nodey[j].a <= i && nodey[j].b >= i) {
                rey[numb] = i;
                k = 1;
                break;
            }
        }
        if(!k)
            return 0;
    }
    return 1;
}
int main() {
    while(cin >> n && n) {
        memset(rex, 0, sizeof(rex));
        memset(rey, 0, sizeof(rey));
        for(int i = 0; i < n; i++) {
            cin >> nodex[i].a;
            cin >> nodey[i].a;
            cin >> nodex[i].b;
            cin >> nodey[i].b;
            nodex[i].num = i+1;
            nodey[i].num = i+1;
        }
        sort(nodex, nodex+n, cmp);
        sort(nodey, nodey+n, cmp);
        if(sol()) {
            for(int i = 1; i <= n; i++) {
                cout << rex[i] << " " << rey[i] << endl;
            }
        }
        else {
            cout << "IMPOSSIBLE" << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值