General problem Solving Techniques [intermediate-1]~d-fabled Rooks

Source: Internet
Author: User

We would like-to-place n rooks, 1≤n≤5000, on a nxn
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 rightlower
Corner (XRI
, Yri), where 1≤i≤n, 1≤xli≤
Xri≤n, 1≤yli≤yri≤n.
• No-rooks can attack each other, which is No and both 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 ' in a line by itself.
Output
Your task is to find such a placing of rooks, the above conditions is 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
is multiple solutions, any one would 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
3 ·
8 5
6 6
3 7
1 1
5 8
2 4
4 2
9}
8 5
6 6
3 7

problem-solving ideas: The title means to put n cars on the N*n board, yes, no two vehicles attack each other, and the first car is within a given rectangular ri. The key point of the problem is that each car's x-coordinate and y-coordinate can be considered separately (they do not affect each other), you can first consider the distribution of vehicles in the same column of different rows, and then the column allocation. Make them stagger each other.

Program code:

#include"String.h"#include"Stdio.h"#include"Algorithm"#include"Iostream"UsingNamespace Std;Constint N=5005;int x1[N],x2[N],y1[N],y2[N],x[N],y[N];IntFunc(Int*a,Int*b,Int*c,int n){Fill(cC+n,-1);For(int I=1; I<=n; I++){int left=-1, right=n+1;For(Int J=0; j<n; j++){If(c[j]<0&&i>=a[j]&&b[j]<right){Left=j; Right=b[j];}}If(left<0|| I>right)ReturnFalse; C[Left]=i;}ReturnTrue;}IntMain(){int T;While(scanf("%d", &t) &&t){For(int I=0; I<t; I++)scanf("%d%d%d%d", &x1[I],&y1[I],&x2[I],&y2[I]);If(Func(x1, x2XT) &&Func(y1, y2YT)){For(int I=0;i<t;i< Span class= "Sh-symbol" >++) printf ( "%d" D\n ",x[i< Span class= "Sh-symbol" >],y[i);  else printf  ( "Impossible\n  ");  return 0;< Span class= "Sh-cbracket" >             

General Problem Solving Techniques [intermediate-1]~d-fabled Rooks

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.