11134 - Fabled Rooks
2016-06-07 14:05
218 查看
Fabled Rooks
We would like to place n rooks, 1≤n≤5000, on a n×n board subject to the following restrictionsThe 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 1≤i≤n, 1≤xli≤xri≤n, 1≤yli≤yri≤n.
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
81 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 15 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
你的任务是在n∗n的棋盘上放n(n≤5000)个车,使得任意两个车不相互攻击,且第i个车在一个给定的矩形Ri之内。用4个整数xli,yli,xri,yri(1≤xli≤xri≤n,1≤yli≤yri≤n)描述第i个矩形,其中(xli,yli)是左上角坐标,(xri,yri)是右下角坐标,则第i个车的位置(x,y)必须满足xli≤x≤xri,yli≤y≤yri。如果无解,输出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; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge