Rent-A-Pixel
Time limit3sMemory limit128 MB
Compute the smallest row- and column-convex block set containing the given blocks and print its outline corners clockwise.
Problem
Harriet Emmel rents out space on her web page in blocks of 10 pixels by 10 pixels. The page is cut by grid lines drawn every 10 pixels, and anyone may rent blocks of that grid to post artwork, advertising, or anything else.
Emmel expected most customers to ask for a rectangle of blocks, but some had other ideas. An optician picked blocks shaped like a pair of eyeglasses, and an archery shop picked blocks shaped like a bullseye.
To keep the bookkeeping simple, Emmel decided that every purchase has to be one orthogonally convex set of blocks. Orthogonally convex means that every row and every column of the grid meets the set in either nothing or one contiguous run of blocks.
A customer picks whatever blocks they want, and Emmel then works out the smallest orthogonally convex region containing all of them and rents out that region. Write the program that computes this region.
Every request in the input gives a smallest region that is connected: for any two pixels of the region there is a path between them that steps up, down, left, or right and never leaves the region.
Input
The input holds several test cases.
The first line of a test case holds the number of blocks the customer picked, (). One or more lines follow, holding integers in the order . Here and are the row and the column of the upper left pixel of block . Every coordinate is a multiple of 10 and satisfies . No coordinate pair appears twice inside one test case.
Each test case is chosen so that the smallest orthogonally convex region containing the blocks is a single connected polygon.
A line holding a single 0 ends the input.
Output
Print one line per test case. Start the line with Case k: , where is the test case number counting from 1. Then print the corners of the smallest orthogonally convex region, each one as its row number followed by its column number. Separate the numbers with single spaces and do not repeat the first corner at the end.
The first corner is the upper left pixel of the block that has the smallest row number, and among those the smallest column number. Starting there, walk once around the outline of the region in the clockwise direction: right along the top edge, down along the right edge, left along the bottom edge, up along the left edge. The walk stays on pixels that belong to the region. Print the row number and the column number of every pixel where the walk changes direction.