User:Jeff/ACM/Eyeball Benders

Back to ACM page

Eyeball Benders
"Eyeball benders" are a popular kind of puzzle in which the reader must identify a common object based on a close-up view of a part of that object. For instance, an image that looks like a regular array of colored cones might be a view of an open box of new crayons. Figure 1 shows an example where the puzzle is on the left and the solution is on the right.

You must verify solutions to a simplified version of the "eyeball bender" puzzle. You will be given a number of pairs of images, each one a collection of line segments. All line segments will be either horizontal or vertical, and they include their endpoints. Figure 2 shows an example.

You must determine whether the images form a valid pair in which the first image is a magnified view of some portion of the second image. Lines are assumed to have zero thickness in both images. At least one endpoint in the puzzle image of a valid pair must be an endpoint of a line segment in the solution image.

Coordinates describe relative positions and scale within a single image. The coordinates in one image do not necessarily use the same origin or scale as those in the other image. The magnification of the puzzle image relative to the solution image is required to be greater than or equal to 1. For Figure 2, your program should determine that this is a valid puzzle/solution image pair.

Input
The input consists of multiple cases. The input for each case begins with two positive integers M and N, (1 &le; M,N &le; 50). M is the number of line segments in the puzzle image. N is the number of line segments in the proposed solution image. The following lines contain M + N pairs of points. The first M pairs of points are the endpoints of the line segments in the puzzle image; the remaining N pairs are the endpoints of the line segments in the proposed solution image. The x and y coordinates for each pair satisfy -100 &le; x,y &le; 100 and are given to at most three decimal places of precision. All input values are separated by white space (blanks or new line characters).

No pair of distinct points in a given image will be closer than .005 to another (relative to the scale of the image) and all segments will have length at least .005. No two horizontal segments overlap and no two vertical segments overlap. However, horizontal segments may intersect vertical segments either internally or at segment endpoints.

The input data for the last case is followed by a line consisting of the integers 0 0.

Output
For each input case, display the case number (1, 2, ...) followed by the words  if the proposed solution image matches a closed rectangular sub-region of the puzzle image (including at least one endpoint), magnified by a factor of one or greater, and possibly translated by some amount. Line segments that are not included in the puzzle image will be at least 0.005 distant from the rectangle.

If the match condition fails to hold, print. Follow the format of the sample output.

Analysis
No team was able to solve this problem at the ICPC world finals. With a little thought, however, some aspects of the algorithm can be reasoned out.

There are at most 50 line segments in both the puzzle and solution images. We are guaranteed that one of the endpoints in the puzzle is a valid endpoint in the solution (if the puzzle is valid). Attempting to match each endpoint in the puzzle image to an endpoint in the solution image is only O(n2), on the order of 10,000 in the worst case.

Furthermore, the segments are all rectilinear (parallel to the x-axis or y-axis). By creating sorted arrays of x-coordinates and y-coordinates, the line segments could be mapped out like on a sheet of graph paper. The rows are the y-coordinates and the columns are the x-coordinates observed in the input data. By observing the consecutive differences in x's and y's for the puzzle and solution arrays, we can calculate a list of possible magnification factors. Testing each magnification factor at each endpoint-to-endpoint matching may, in fact, run in time. (But this doesn't mean that it's trivial to program!)