Finding Rectangles

Time limit1sMemory limit128 MB

Summary
List every axis-aligned rectangle that can be formed from up to 26 labeled points, printing the four vertex labels in clockwise order.
Level

Medium4 of 10

Topics
Brute force, Geometry, Sorting, Implementation
Solved
No attempts yet

Problem

Consider the point sets in figures 1a, 2a, and 3a. Using only those points as vertices, figures 1b, 2b, and 3b show every rectangle that can be formed with horizontal and vertical sides. No rectangle can be formed from the points in figure 4.

Write a program that finds every rectangle with horizontal and vertical sides (that is, axis-aligned) that can be formed from a given set of labeled points. The example cases below correspond to the figures above.

Input

The input contains one or more point sets, followed by a line containing a single 00 that marks the end of the input.

Each point set begins with a line containing nn, the number of points, followed by nn lines that describe the points. Each point description contains a capital letter (the label of the point), a space, the horizontal coordinate, a space, and the vertical coordinate.

Within each set the point labels appear in alphabetical order. Because each point is labeled with a capital letter, a set contains at most 26 points. All coordinates are nonnegative integers less than 50, and the points within a set are distinct.

Output

For each point set, print Point set followed by the number of the point set and a colon.

If the set contains no rectangles, print No rectangles (with a single leading space) after the colon, on the same line.

Otherwise the rectangles are listed starting on the next line. A single space precedes each rectangle. Each rectangle is given by its four vertex labels in clockwise order starting from the upper-left corner, that is upper-left, upper-right, lower-right, lower-left (a larger vertical coordinate is higher). The rectangles are printed ten per line, except possibly the last line, and are listed in alphabetical order.

Examples1

  1. Example 1

    Input
    7
    A 1 1
    B 2 1
    C 3 1
    D 2 3
    E 3 3
    F 1 4
    G 3 4
    8
    B 1 1
    D 2 1
    F 4 1
    J 4 4
    L 2 4
    M 2 3
    N 4 3
    P 1 2
    12
    A 1 5
    B 2 5
    C 1 4
    D 2 4
    E 1 3
    F 2 3
    G 1 2
    H 2 2
    I 1 1
    J 2 1
    K 1 0
    L 2 0
    5
    B 1 1
    D 2 1
    L 2 4
    N 2 3
    P 1 2
    0
    
    Expected output
    Point set 1:
     DECB FGCA
    Point set 2:
     LJFD LJNM MNFD
    Point set 3:
     ABDC ABFE ABHG ABJI ABLK CDFE CDHG CDJI CDLK EFHG
     EFJI EFLK GHJI GHLK IJLK
    Point set 4: No rectangles