UFO Landing Field (UFO) 5
Time limit1sMemory limit1024 MB
Place as many small fixed-shape UFOs as possible in a grid of landable cells, with the twist that no two placed UFOs may share an edge.
- Level
Hard8 of 10
- Topics
- Brute force, Greedy, Implementation, Matrix
- Solved
- No attempts yet
Problem
The aliens of Planet IOI are planning to build a UFO landing field in JOI Park in Japan (whose existence is kept secret from the general public). The aliens surveyed JOI Park and produced a map marking the places where a UFO can land.
JOI Park is a rectangle W meters east to west and H meters north to south, divided into 1 meter by 1 meter square cells. There are W × H cells in total, and the cell in the x-th column from the west and the y-th row from the north is written (x, y). The northwest corner cell is (1, 1), and the southeast corner cell is (W, H). Each cell is either "landable" or "not landable", written "." for "landable" and "w" for "not landable".
A UFO built by the aliens of Planet IOI fits within B meters in width and D meters in depth. By its specifications, the UFO must land so that the up, down, left, and right directions on the blueprint become north, south, west, and east on the UFO respectively, and so that the top-left corner of the blueprint coincides with the top-left corner of some cell of JOI Park. The UFO blueprint consists of a grid of D rows and B columns. There are B × D squares in total, and the square in the i-th column from the left and the j-th row from the top is written (i, j). The top-left square is (1, 1), and the bottom-right square is (B, D). Square (i, j) indicates whether the square region of the UFO from i − 1 meters to i meters from the west end and from j − 1 meters to j meters from the north end contains a part of the UFO. Each square is either "contains" or "does not contain" a part of the UFO, written "O" (capital letter O) if it contains one and "." if it does not.
When one UFO lands, every place on the blueprint marked as "contains" a part of the UFO must be contained in a "landable" cell at landing time. Also, when multiple UFOs land, every place marked as "contains" a part of the UFO on one UFO's blueprint cannot share an edge with any place marked as "contains" a part of the UFO on another UFO's blueprint. However, UFOs may land at places that share a corner.
By chance, you were chosen as the goodwill ambassador of Planet IOI. Therefore, you are asked to create a plan for a landing field that allows as many UFOs to land as possible.
Create a plan for the UFO landing field to be built in JOI Park. The more UFOs your plan lands, the higher your score. The plan is the map of JOI Park with the landed UFOs drawn on it; among the "landable" cells of JOI Park, cells contained in a landed UFO are written "O" (capital letter O), cells not contained in any landed UFO are written ".", and "not landable" cells are written "w".
Submit output for each input datum. Only whether the output matches the format specified in the Output section is checked.
Input
The input file is given in the following format.
- The first line contains integers B and D separated by a space, meaning the UFO's width is B meters and its depth is D meters.
- The following D lines contain information about the UFO blueprint. The (j + 1)-th line (1 ≤ j ≤ D) contains a string of B characters, and the i-th character (1 ≤ i ≤ B) is "O" or "." representing square (i, j) of the blueprint.
- The (D+2)-th line contains integers W and H separated by a space, meaning JOI Park's east-west size is W meters and its north-south size is H meters.
- The following H lines contain information about the JOI Park map. The (y + D + 2)-th line (1 ≤ y ≤ H) contains a string of W characters, and the x-th character (1 ≤ x ≤ W) is "." or "w" representing cell (x, y) of the park.
Output
The y-th line of the output (1 ≤ y ≤ H) contains a string of W characters representing the state of the y-th row from the north of the park. The x-th character (1 ≤ x ≤ W) of the y-th line (1 ≤ y ≤ H) is one of "O", ".", or "w", representing the state of cell (x, y).
Constraints
- 1 ≤ B ≤ 5: width of the UFO (meters)
- 1 ≤ D ≤ 5: depth of the UFO (meters)
- 1 ≤ W ≤ 200: east-west size of JOI Park (meters)
- 1 ≤ H ≤ 200: north-south size of JOI Park (meters)
Hint
This output example is a plan that lands 11 UFOs.