This page is still under construction.

Parts of this page are still being built. What you see may change.

Stars

Time limit1sMemory limit128 MB

Summary
Given a star map of points with brightness and several constellation point sets, count how many rotated or scaled copies of each occur and report the brightest such occurrence.
Level

Medium6 of 10

Topics
Geometry, Hash map, Brute force, Sorting
Solved
No attempts yet

Problem

On a clear, moonless night you can see millions of stars glimmering in the sky. Faced with this overwhelming number, the Greeks began — nearly 2,000 years ago — to bring some order to the chaos. They grouped stars into constellations and gave them names that are still in use today, such as "Ursa Minor", "Pisces", and "Cancer".

Given only a sketch of a constellation, an amateur cannot easily find it in the real sky. Worse, the shape of a simple constellation — for example "Triangulum", a triangle of just three stars — may appear many times, and singling out the correct occurrence is hard.

In this problem we let the computer find constellations in the sky.

You are given a star map: a set of points in the plane, each with an associated brightness. You are also given a constellation, again as a set of points in the plane. For the constellation you must report:

  • how many times it occurs in the star map, and
  • the position of its brightest occurrence, if any occurrence exists. (The idea: if a constellation seems to appear several times, the brightest one is most likely the real one, because it is the most eye-catching.)

An occurrence is a subset of the map's stars that forms an arbitrarily rotated and/or scaled copy of the constellation's stars (translation is also allowed; reflections are not). A given subset may fit the constellation in several orientations — a square fits in 4 ways under 90∘90^\circ rotations — but such rotations of the same subset do not count as separate occurrences.

The brightness of an occurrence is the average brightness of its stars: the sum of the individual brightnesses divided by the number of stars in the constellation.

Input

The input describes several star maps.

Each map begins with a line containing a single integer NN — the number of stars in the map (1≤N<10001 \le N < 1000). The next NN lines each contain three integers: the XX- and YY-coordinates and the brightness of one star. A larger value means a brighter star.

The following line contains a single integer MM — the number of constellations to process (1≤M<501 \le M < 50). Each constellation description begins with a line containing an integer SS (the number of stars in the constellation) and a string CC (its name; at most 40 characters, containing no blanks). The next SS lines each contain the XX/YY-coordinates of one constellation star.

All coordinates lie in the range −1000-1000 to 10001000; brightness values lie in the range 00 to 100100.

A blank line separates one star map from the next. The input ends with an empty map (N=0N = 0), which must not be processed.

Note: because all star coordinates are integers, you can immediately reject any rotated or scaled placement of the constellation whose points would not land on integer coordinates.

Output

For each star map, first print the map number on its own line: Map #1 for the first map, Map #2 for the second, and so on.

Then, for each constellation — in the same order as the input — print one line of the form <name> occurs <k> time(s) in the map., where <name> is the constellation's name and <k> is the number of times it occurs.

If <k> is at least 1, print on the next line Brightest occurrence: followed by the positions of the stars of the brightest occurrence, each written as (x,y) and separated by single spaces. Print the positions in ascending order of XX-coordinate; stars sharing the same XX-coordinate are ordered by ascending YY-coordinate. You may assume that every constellation has a single, unique brightest occurrence in each map.

Print a blank line before each constellation, and a line of five dashes (-----) after each star map.

Examples3

  1. Example 1

    Input
    6
    1 2 1
    2 1 4
    2 4 3
    3 2 1
    4 1 5
    4 3 2
    2
    3 Triangulum
    1 1
    3 1
    2 4
    4 Cancer
    1 3
    4 3
    6 1
    7 5
    
    0
    
    Expected output
    Map #1
    
    Triangulum occurs 2 time(s) in the map.
    Brightest occurrence: (1,2) (4,1) (4,3)
    
    Cancer occurs 0 time(s) in the map.
    -----
    
  2. Example 2

    Input
    4
    0 0 1
    2 0 2
    2 2 3
    0 2 4
    1
    4 Square
    0 0
    1 0
    1 1
    0 1
    0
    
    Expected output
    Map #1
    
    Square occurs 1 time(s) in the map.
    Brightest occurrence: (0,0) (0,2) (2,0) (2,2)
    -----
    
  3. Example 3

    Input
    3
    0 0 10
    2 0 20
    0 1 30
    1
    3 Alpha
    0 0
    2 0
    0 1
    4
    0 0 1
    4 0 2
    4 4 3
    0 4 4
    1
    4 Box
    0 0
    1 0
    1 1
    0 1
    0
    
    Expected output
    Map #1
    
    Alpha occurs 1 time(s) in the map.
    Brightest occurrence: (0,0) (0,1) (2,0)
    -----
    Map #2
    
    Box occurs 1 time(s) in the map.
    Brightest occurrence: (0,0) (0,4) (4,0) (4,4)
    -----