Watering the fields
Time limit1sMemory limit128 MB
Cover every non-scarecrow cell with trominoes of three cells while letting at most R times C trominoes cross field borders.
- Level
Hard8 of 10
- Topics
- Implementation, Backtracking, Combinatorics
- Solved
- No attempts yet
Problem
Sara farms a large rectangle of land. The land is a grid of rows and columns of cells. 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 plots of size , called fields.
Some fields have a scarecrow to keep birds off the crops. A scarecrow occupies one cell, and each field has at most one scarecrow.
In a drought Sara waters the crops with sprinklers. Each sprinkler has one main nozzle and two side nozzles. It occupies exactly three cells and waters all three. Each side nozzle sits on a cell that shares an edge with the main nozzle (up, down, left, or right). So every sprinkler has one of these six shapes:
- three cells in a vertical line
- three cells in a horizontal line
- an L made of a square minus one cell (four orientations)
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 need not lie in the same field. They may straddle neighboring fields. In that case Sara drills a hole in the fence between the two cells that the same sprinkler waters. Drilling is hard, so the number of holes must be at most .
A valid placement always exists. Output a valid placement with at most holes.
Input
The first line contains two integers and ().
Then lines follow, each with characters. They show the fields and the fences between them. The fence is infinitely thin in the land, but the input still draws it with characters.
. is an empty cell, # (ASCII 35) is a scarecrow, | (ASCII 124) is a vertical fence, - is a horizontal fence, and + is a fence crossing.
Output
Print a grid of the same size with a valid sprinkler placement. Mark each hole with _. Replace every . from the input with a lowercase letter a to z so that:
- The three cells of one sprinkler share the same letter, even when they are not all in the same field.
- If two adjacent cells in the same field belong to different sprinklers, they must have different letters.
- If two adjacent cells in different fields belong to different sprinklers and there is a hole in the fence between them, they must have different letters.
- Adjacent cells in different fields may share a letter as long as the rules above hold.
The number of holes must be at most .