11134 - Fabled Rooks

Fabled Rooks

We would like to place n rooks, 1n5000 , on a n×n board subject to the following restrictions

  • The i -th rook can only be placed within the rectangle given by its left-upper corner (xli,yli) and its right-lower corner (xri,yri) , where 1in , 1xlixrin , 1yliyrin .
  • No two rooks can attack each other, that is no two rooks can occupy the same column or the same row.

Input

The input consists of several test cases. The first line of each of them contains one integer number, n , the side of the board. n lines follow giving the rectangles where the rooks can be placed as described above. The i -th line among them gives xli , yli , xri , and yri . The input file is terminated with the integer ‘0’ on a line by itself.

Output

Your task is to find such a placing of rooks that the above conditions are satisfied and then output n lines each giving the position of a rook in order in which their rectangles appeared in the input. If there are multiple solutions, any one will do. Output ‘IMPOSSIBLE’ if there is no such placing of the rooks.

Sample Input

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

Sample Output

1 1
5 8
2 4
4 2
7 3
8 5
6 6
3 7
1 1
5 8
2 4
4 2
7 3
8 5
6 6
3 7

你的任务是在 nn 的棋盘上放 n(n5000) 个车,使得任意两个车不相互攻击,且第 i 个车在一个给定的矩形 Ri 之内。用4个整数 xli,yli,xri,yri1xlixrin1yliyrin) 描述第 i 个矩形,其中 (xli,yli) 是左上角坐标, (xri,yri) 是右下角坐标,则第 i 个车的位置 (x,y) 必须满足 xlixxri yliyyri 。如果无解,输出IMPOSSIBLE;否则输出 n 行,依次为第1,2,…,n个车的坐标。

#include <iostream>
#include <cstring>

using namespace std;

const int maxNum = 5000 + 5;
int n;
int xl[maxNum], yl[maxNum], xr[maxNum], yr[maxNum];
int ansX[maxNum], ansY[maxNum];

bool solve(int *a, int *b, int *c) {
    // 填满1-n
    for(int col = 1; col <= n; col++) {
        // 第几个车
        int rook = -1;
        int minb = n + 1;
        // 找到覆盖col,且右区间最小的车
        for(int i = 0; i < n; i++) {
            if(c[i] < 0 && b[i] < minb && a[i] <= col) {
                rook = i;
                minb = b[i];
            }
        }
        // 没有车覆盖该col或者在1-col间有重复车
        if(rook < 0 || col > minb) {
            return false;
        }
        // 该车占据col
        c[rook] = col;
    }
    return true;
}

int main() {
    while(cin >> n && n) {
        for(int i = 0; i < n; i++) {
            cin >> xl[i] >> yl[i] >> xr[i] >> yr[i];
        }

        // 初始化
        memset(ansX, -1, sizeof(ansX));
        memset(ansY, -1, sizeof(ansY));

        if(solve(xl, xr, ansX) && solve(yl, yr, ansY)) {
            for(int i = 0; i < n; i++) {
                cout << ansX[i] << " " << ansY[i] << endl;
            }
        } else {
            cout << "IMPOSSIBLE" << endl;
        }
    }
    return 0;
}

回溯法超时

#include <iostream>
#include <cstring>

using namespace std;
// 回朔法超时
const int maxNum = 5000 + 5;
int n;
int xl[maxNum], yl[maxNum], xr[maxNum], yr[maxNum];
int ansX[maxNum], ansY[maxNum];
int markR[maxNum], markC[maxNum];

bool dfs(int cur) {
    if(cur == n) {
        return true;
    } else {
        for(int i = xl[cur]; i <= xr[cur]; i++) {
            for(int j = yl[cur]; j <= yr[cur]; j++) {
                if(!markR[i] && !markC[j]) {
                    // 标记行列为已访问
                    markR[i] = markC[j] = 1;
                    // 设置结果
                    ansX[cur] = i;
                    ansY[cur] = j;
                    // 递归访问
                    if(dfs(cur + 1)) {
                        return true;
                    }
                    // 回溯
                    markR[i] = markC[j] = 0;
                }
            }
        }
    }
    return false;
}

int main() {
    while(cin >> n && n) {
        for(int i = 0; i < n; i++) {
            cin >> xl[i] >> yl[i] >> xr[i] >> yr[i];
        }

        // 初始化
        memset(markR, 0, sizeof(markR));
        memset(markC, 0, sizeof(markC));

        int cur = 0;
        if(dfs(cur)) {
            for(int i = 0; i < n; i++) {
                cout << ansX[i] << " " << ansY[i] << endl;
            }
        } else {
            cout << "IMPOSSIBLE" << endl;
        }
    }
    return 0;
}

你可能感兴趣的:(ACM,uva,UVa11134)