Sprinkler layout
Time limit1sMemory limit128 MB
Sara tiles her fenced farm with tromino sprinklers around scarecrows using at most as many fence holes as fields.
- Level
Medium7 of 10
- Topics
- Implementation, Simulation, Backtracking, Greedy
- Solved
- No attempts yet
Problem
Sara owns a rectangular farm. The farm is a grid of rows and columns of cells. A horizontal fence crosses the farm after every fifth row, and a vertical fence crosses the farm after every fifth column. The fences split the farm into plots of size , called fields.
Birds and drought are the two problems Sara deals with. To keep birds off the crops, some fields have a scarecrow. A scarecrow occupies one cell, and a field has at most one scarecrow.
During 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. The two side nozzles sit on cells that are orthogonally adjacent to the main nozzle. So the three cells form a straight tromino or an L tromino.
Sara wants exactly one sprinkler nozzle on every cell that does not hold a scarecrow. A scarecrow cell must not hold a nozzle. Nozzles must not sit outside the farm.
The three cells of one sprinkler may lie in neighboring fields. Then Sara must drill a hole in the fence between the two cells of that sprinkler that sit in different fields. Drilling is hard, so the arrangement must use at most holes.
Given the farm, print a valid sprinkler arrangement. A valid arrangement always exists.
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, but the input still writes it with characters.
Each cell is one character. A dot . is an empty cell, and # (ASCII 35) is a scarecrow. Vertical fences are | (ASCII 124), horizontal fences are -, and a fence crossing is +.
Output
Print a grid in the same layout as the input. Mark each hole in a fence with _. Replace every empty cell . from the input with a lowercase letter a through z so that these rules hold.
- The three cells watered by the same sprinkler share the same letter, even if they are not all in the same field.
- If two adjacent cells in the same field are watered by different sprinklers, they must have different letters.
- If two adjacent cells in different fields are watered by 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 previous rules hold.
If several arrangements are valid, print the one obtained by processing fields in snake order. Rows are numbered from . Even rows go left to right, odd rows go right to left. When a field still has a number of uncovered empty cells that is not a multiple of , drill one hole to the next neighboring field in that order and lay one sprinkler through the hole so the current field becomes a multiple of . Tile the rest of each field by repeatedly covering the topmost empty cell, and the leftmost one if there is a tie. Label sprinklers in the order they are placed with the earliest letter that does not break the lettering rules against already labeled neighbors. For the sample input, print the sample output exactly.
Hint
A field with no scarecrow has cells, which is not a multiple of , so it cannot be covered by sprinklers alone. A hole in the fence is needed so a sprinkler can be shared with a neighboring field. A field with a scarecrow has empty cells and can be covered inside itself.