UVa 11134-fabled rooks--[problem decomposition, greedy method]

Source: Internet
Author: User

We would like-to-place n rooks,1≤n≤ the, on a nxnboard subject to the following restrictions the I-th rook can only is placed within the Rectanglegiven by it left-Upper Corner (XLI, Yli) and its rightlowercorner (Xri, Yri),where 1≤i≤n,1≤xli≤xri≤n,1≤yli≤yri≤n. No rooks can attack each other, that isNo. Rookscan occupy the same column or the same row. Input The input consists of several test cases. The first line of Eachof them contains one integer number, n, the side of the board. N lines follow giving the rectangleswhereThe rooks can be placed asdescribed above. The I-th line among them gives XLI, Yli, Xri, Andyri.
The input file isTerminated with the integer '0' on a line by itself. Output Your Task isTo find such a placing of rooks that the above conditions is satisfied and then output nlines each giving the Positi On a rookinchOrderinchWhich their rectangles appearedinchThe input. If thereare Multiple solutions, any one would Do. Output ' Impossible 'ifThere isno 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 881 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 80Sample Output1 15 82 44 27 38 56 63 71 15 82 44 27 38 56 63 7

Problem Solving Ideas:

After analysis, it can be found that the cross-ordinate selection of each vehicle is independent of each other, so it can be considered separately. Then the problem can be reduced to: put the 1~n into [xli,xri]n interval] respectively. The chosen method uses the greedy method--by the behavior example, first the each rectangular interval presses the XR from small to the big order [XL1,XR1][XL2,XR2] ... [XLN,XRN]; Select the position of the unplaced vehicle in each interval as far as possible by the XL, for example [XL1,XR1], the car should be placed in the XL1 position, at this time for the interval [xl2,xr2],xl1 position no longer considered.

The code is as follows:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <ctime>5#include <algorithm>6 using namespacestd;7 #definetime__ printf ("Time:%f\n", double (Clock ())/clocks_per_sec)8 Const intmaxn= the;9 structrec{Ten     intXl,yl,xr,yr; One }; ARec rect[maxn+5]; - intid[maxn+5]; - intid_row_num[maxn+5]; the intid_col_num[maxn+5]; - introw[maxn+5]; - intcol[maxn+5]; - intN; + voidinit () { -memset (Id_row_num,0,sizeofid_row_num); +memset (Id_col_num,0,sizeofid_col_num); Amemset (Row,0,sizeofrow); atmemset (Col,0,sizeofcol); - } - BOOLCmp_row (int&a,int&b) { -     returnrect[a].xr<RECT[B].XR; - } - BOOLCmp_col (int&a,int&b) { in     returnrect[a].yr<Rect[b].yr; - } to BOOLSolve_row () { +      for(intI=1; i<=n;i++) -id[i]=i; theSort (id+1, id+1+N, cmp_row); *     //bool Ok=true; $      for(intI=1; i<=n;i++){Panax NotoginsengRec &t=Rect[id[i]]; -         BOOLflag=false; the          for(intj=t.xl;j<=t.xr;j++){ +             if(row[j]==0){ Arow[j]=1; theid_row_num[id[i]]=J; +flag=true; -                  Break; $             } $         } -         if(!flag) { -             return false; the         } -     }Wuyi     return true; the } - BOOLSolve_col () { Wu      for(intI=1; i<=n;i++) -id[i]=i; AboutSort (id+1, id+1+N, cmp_col); $     //bool Ok=true; -      for(intI=1; i<=n;i++){ -Rec &t=Rect[id[i]]; -         BOOLflag=false; A          for(intj=t.yl;j<=t.yr;j++){ +             if(col[j]==0){ thecol[j]=1; -id_col_num[id[i]]=J; $flag=true; the                  Break; the             } the         } the         if(!flag) { -             return false; in         } the     } the     return true; About } the  the intMainintargcConst Char*argv[]) { the      while(SCANF ("%d", &n) = =1&&N) { + init (); -          for(intI=1; i<=n;i++) thescanf"%d%d%d%d",&rect[i].xl,&rect[i].yl,&rect[i].xr,&rect[i].yr);Bayi         if(Solve_row () &&Solve_col ()) { the              for(intI=1; i<=n;i++) theprintf"%d%d\n", Id_row_num[i],id_col_num[i]); -         } -         Elseprintf"impossible\n"); the         //time__; the     } the     //time__; the     return 0; -}

UVa 11134-fabled rooks--[problem decomposition, greedy method]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.