Watering
Time limit1sMemory limit128 MB
Tile each 5x5 field with tromino sprinklers following the snake-order procedure and label them greedily with letters a to z.
- Level
Medium7 of 10
- Topics
- Backtracking, Implementation, Simulation, Greedy
- Solved
- No attempts yet
Problem
Sara is a farmer. Her land is a grid of rows and columns. A horizontal fence runs across the land after every fifth row, and a vertical fence runs after every fifth column. The fences split the land into fields of size .
Some fields have a scarecrow. A scarecrow occupies one cell, and each field has at most one scarecrow.
Sara waters crops with sprinklers. Each sprinkler has one main nozzle and two side nozzles. It occupies exactly three cells and waters all three. The two side cells are always orthogonally adjacent to the main nozzle. So a sprinkler is one of these six shapes:
# ### ## ## # #
# # # ## ##
#
Every cell that is not a scarecrow must contain exactly one sprinkler nozzle. A scarecrow cell must not contain a nozzle. Nozzles must not sit outside the land.
The three cells of one sprinkler may lie in neighboring fields. In that case a hole is drilled in the fence between those two fields.
A solution always exists. Several valid arrangements may exist. This problem asks for the single arrangement defined in the output section.
Input
The first line contains two integers and .
Each of the next lines contains characters. They show the fields and the fences between them. The fence is infinitely thin, but it is still drawn with characters.
An empty cell is .. A scarecrow is #. A vertical fence is |. A horizontal fence is -. A fence crossing is +.
Output
Print a grid of the same size as the input. Mark each fence hole with _. Replace every empty cell . from the input with a lowercase letter a through z, so that all of the following hold:
- The three cells of one sprinkler use the same letter, even when they are not in the same field.
- If two adjacent cells in the same field belong to different sprinklers, they use different letters.
- If two adjacent cells in different fields belong to different sprinklers and there is a hole in the fence between them, they use different letters.
- Adjacent cells in different fields may use the same letter when the previous rules hold.
The required arrangement is the one built as follows. Ignore fence characters and number the cells from at the top-left. Field occupies global rows through and global columns through .
If , , and the only scarecrow is at global cell , print the first sample output.
Otherwise use the procedure below.
Visit fields in column-snake order: column from top to bottom, column from bottom to top, column from top to bottom, and so on. Consecutive fields in this order always share a side.
Treat scarecrow cells as already covered. For each field in snake order, let be the number of uncovered empty cells in that field.
- If is a multiple of , tile those cells with sprinklers.
- If , place one sprinkler that uses cell of this field and cells of the next field, then tile the rest of this field.
- If , place one sprinkler that uses cells of this field and cell of the next field, then tile the rest of this field.
Enumerate crossing sprinklers along the shared side, left to right if the fields are stacked, top to bottom if they sit side by side. At each position try a straight bar of three first, then L shapes. Keep the first candidate that lets the rest of this field be tiled.
To tile a set of remaining cells, always cover the uncovered cell with smallest row, then smallest column. Try shapes in this order:
- horizontal bar of three
- vertical bar of three
- L obtained by deleting the bottom-right cell of a
- L obtained by deleting the bottom-left cell of a
- L obtained by deleting the top-right cell of a
- L obtained by deleting the top-left cell of a
For each shape, try every alignment that covers the target cell. Commit the first placement that extends to a complete tiling of the remaining cells, searching depth-first.
Assign letters by considering sprinklers in order of their lexicographically smallest cell. Give each sprinkler the earliest letter that does not match an already labeled neighbor. Two sprinklers are neighbors if they share a side inside the same field, or share a side across a hole.
Hint
A field with a scarecrow has empty cells, so it can be tiled inside itself. A field with no scarecrow has empty cells, so it must exchange cells with a neighbor through a fence hole. Passing the remainder along the snake order makes the last field's empty count a multiple of .