UVA 11134 Fabled Rooks

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65530#problem/D

解题思路:由于行和列之间是不相互影响的,因此我们对行和列分开进行处理。然后对于每行和每列采用贪心的方法进行安置即可。

#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 5010;
bool mark[maxn];

struct Node {
    int x1, y1, x2, y2, id;
    Node() { }
    Node(int _x1, int _y1, int _x2, int _y2, int _id) : x1(_x1), y1(_y1), x2(_x2), y2(_y2), id(_id) { }
}nd[maxn];

bool comp1(const Node &p1, const Node &p2) {
    if(p1.x2 == p2.x2) {
        return p1.x1 < p2.x1;
    }
    return p1.x2 < p2.x2;
}

bool comp2(const Node &p1, const Node &p2) {
    if(p1.y2 == p2.y2) {
        return p1.y1 < p2.y1;
    }
    return p1.y2 < p2.y2;
}

int x[maxn], y[maxn];

int main() {

    freopen("aa.in", "r", stdin);
    int n;
    bool flag;
    while(scanf("%d", &n) == 1) {
        if(n == 0) break;
        flag = true;
        for(int i = 0; i < n; ++i) {
            nd[i].id = i;
            scanf("%d %d %d %d", &nd[i].x1, &nd[i].y1, &nd[i].x2, &nd[i].y2);
        }
        memset(mark, false, sizeof(mark));
        sort(nd, nd + n, comp1);
        bool ok;
        for(int i = 0; i < n; ++i) {
            ok = false;
            for(int j = nd[i].x1; j <= nd[i].x2; ++j) {
                if(!mark[j]) {
                    x[nd[i].id] = j;
                    mark[j] = true;
                    ok = true;
                    break;
                }
            }
            if(!ok) {
                flag = false;
                break;
            }
        }
        if(flag) {
            sort(nd, nd + n, comp2);
            memset(mark, false, sizeof(mark));
            for(int i = 0; i < n; ++i) {
                ok = false;
                for(int j = nd[i].y1; j <= nd[i].y2; ++j) {
                    if(!mark[j]) {
                        y[nd[i].id] = j;
                        mark[j] = true;
                        ok = true;
                        break;
                    }
                }
                if(!ok) {
                    flag = false;
                    break;
                }
            }
        }
        if(flag) {
            for(int i = 0; i < n; ++i) {
                printf("%d %d\n", x[i], y[i]);
            }
        } else {
            printf("IMPOSSIBLE\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值