UVA11134- Fabled Rooks

题目链接


题意:在n*n棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定的矩形之内。

思路:刚开始以为是n皇后的问题,但是本题只要水平和竖直才能攻击到,并没有斜线的约束。所以可以判断出行和列是互相没有影响的,那么只要分别对行和列进行贪心操作,先按照左端点值从小到大排序,然后用优先队列维护,先处理右端点小的。

做法与这题类似 UVA1422


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;

const int MAXN = 5005;

struct Car{
    int l, r, id;
    friend bool operator < (const Car a, const Car b) {
        return a.r > b.r; 
    }
}x[MAXN], y[MAXN], c[MAXN];

int n;

int cmp(Car a, Car b) {
    return a.l < b.l;
}

int judge(Car *cur, int flag) {
    sort(cur, cur + n, cmp);
    priority_queue<Car> q;
    Car state;

    int cnt = 0;
    for (int i = 0; i < n; i++) {
        while (cnt < n && cur[cnt].l <= i + 1) 
            q.push(cur[cnt++]);
        if (q.empty()) {        
            return false; 
        }
        state = q.top(); 
        q.pop();
        if (state.r < i + 1) {
            return false; 
        }
        if (flag == 0)
            c[state.id].l = i + 1;
        else
            c[state.id].r = i + 1;
    }
    return true;
}

void outPut() {
    for (int i = 0; i < n; i++)
        printf("%d %d\n", c[i].l, c[i].r);
}

int main() {
    while (scanf("%d", &n) && n) {
        for (int i = 0; i < n; i++) {         
            scanf("%d%d%d%d", &x[i].l, &y[i].l, &x[i].r, &y[i].r);
            x[i].id = y[i].id = i;
        }
        if (judge(x, 0) && judge(y, 1))
            outPut();
        else
            printf("IMPOSSIBLE\n");
    }    
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值