Fabled Rooks,fabledrooks
Problem F: Fabled Rooks We would like to placen rooks, 1 ≤ n ≤ 5000, 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 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.
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. Thei-th line among them gives xli, yli,xri, and yri. The input file is terminated with the integer `0' on a line by itself.
Your task is to find such a placing of rooks that the above conditions are satisfied and then outputn 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. OutputIMPOSSIBLE if there is no such placing of the rooks.
Sample input
81 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 80
Output for sample input
1 15 82 44 27 38 56 63 7
題意:在棋盤上將8棋子放入棋盤中;且不能再同一行或者同一列;還要滿足的一個條件就是他給出的每一個地區都必有一個棋子;
方法:可以先看行,再看列;再輸入測試資料是給出來的答案可能不一樣,但是不影響最後的結果;
# include <cstdio># include <iostream>using namespace std;int x1[5005],x2[5005],y1[5005],y2[5005],x[5005],y[5005];bool solve(int *x1,int *x2,int *x,int n){ fill(x,x+n,-1); //將值置為-1; for(int cool=1;cool<=n;cool++) //列舉要填的數: { int root=-1,Max=n+1; for(int i=0;i<n;i++) //判斷邊界,找出下邊界最小的那個; if(x[i]<0 && x2[i]<Max && x1[i] <= cool ) { root=i; Max=x2[i]; } if(root<0||cool>Max) return false; //越界或者沒有這個區間; x[root]=cool; } return true;}int main (){ int n,i,j; while(cin>>n&&n) { for(i=0;i<n;i++) scanf("%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i]); if(solve(x1,x2,x,n)&&solve(y1,y2,y,n)) for(i=0;i<n;i++) printf("%d %d\n",x[i],y[i]); else printf("IMPOSSIBLE\n"); } return 0;}