首页 > 代码库 > UVA11134- Fabled Rooks

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;
}