#P2741. Stacking Rectangles
Stacking Rectangles
Stacking Rectangles
You are given a composite image produced by stacking several rectangular images. Each rectangle is drawn using a unique uppercase letter and has the following properties:
- The border has a width of 1 and each side has length at least 3.
- At least a portion of every edge (including corners) of the rectangle must be visible in the composite image.
- If a rectangle is placed later (i.e. on top) than another one, then in every cell where it is drawn its letter will cover the letter of any rectangle below.
The stacking process is as follows: the rectangles are placed one over the other in a given order. For example, if the rectangles are numbered 1 to 5 (from bottom to top), then rectangle 1 is placed first and rectangle 5 is placed last. When a rectangle covers portions of a rectangle below, those parts become invisible.
You are given the final composite image, and your task is to determine the order in which the rectangular images were stacked, from bottom to top. This order is represented by the letters corresponding to each rectangle. In other words, if rectangle X is completely or partially covered in some portions by rectangle Y, then X must have been placed before Y.
Note: All formulas should be written in \( \text{\LaTeX} \) format. For instance, the rectangle dimensions are given by \(9 \times 8\) and the numbering is \(1 \sim 5\).
inputFormat
The first line contains two integers \(R\) and \(C\) (the number of rows and columns of the image). Each of the following \(R\) lines contains a string of length \(C\), representing a row of the composite image. Each character is either a period ('.'), representing an empty cell, or an uppercase letter which represents part of a rectangle's border.
outputFormat
Output a single string that lists the letters corresponding to the rectangles in the order they were stacked, from bottom to top.
sample
9 8
.CCC....
ECBCBB..
DCBCDB..
DCCC.B..
D.B.ABAA
D.BBBB.A
DDDDAD.A
E...AAAA
EEEEEE..
EDABC