Sprinkler Layout
Time limit1sMemory limit128 MB
Tile every empty cell with 3-cell sprinklers in the prescribed row-major order, then drill fence holes and assign letters a to z greedily.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
Sara farms a large rectangular piece of land. The land is a grid of rows and columns. A horizontal fence crosses the land after every fifth row and a vertical fence crosses it after every fifth column, so the fences cut the land into areas of cells called fields.
Birds eat the crops, so some fields have a scarecrow. A scarecrow takes one cell, and a field holds at most one scarecrow.
During a drought Sara waters the crops with sprinklers. A sprinkler has one main nozzle and two side nozzles. The main nozzle takes one cell, and the two side nozzles take two different cells that touch the main nozzle from above, from below, from the left, or from the right. A sprinkler therefore takes three cells and waters all three of them.
Sara wants exactly one nozzle on every cell that has no scarecrow. A cell with a scarecrow takes no nozzle, and no nozzle may lie outside the land.
The three cells that one sprinkler waters need not lie in the same field. When they reach into a neighboring field, Sara drills a hole in the fence between the two cells.
Given the land, print an arrangement of sprinklers.
Input
The first line contains two integers and (), the size of the land.
Then lines follow, each with characters. They describe the land and the fences between the fields. The fence is infinitely thin, but it still takes one character in the picture.
One cell is one character. An empty cell is . and a cell with a scarecrow is #. A vertical fence is |, a horizontal fence is -, and a crossing of two fences is +.
A field holds at most one scarecrow. The data is such that the procedure described in the output section runs to the end.
Output
Print lines of characters, showing the land with the sprinklers in the same format as the input. Keep # on every cell with a scarecrow, replace every empty cell with a lowercase letter from a to z, and replace the fence character with _ at every hole. The arrangement satisfies these four rules.
- The three cells that one sprinkler waters carry the same letter, even when they do not all lie in the same field.
- When two cells that touch inside one field are watered by different sprinklers, they carry different letters.
- When two cells that touch across a fence are watered by different sprinklers and the fence between them has a hole, they carry different letters.
- Two cells that touch across a fence may carry the same letter as long as the rules above hold.
Several arrangements can satisfy the rules, so print the one that this procedure builds. Rows are counted from the top and columns from the left.
Read the cells from top to bottom, and from left to right inside a row. When you reach a cell that has no scarecrow and that no sprinkler waters yet, put a sprinkler on it. Use the first shape in this list whose three cells all lie inside the land, hold no scarecrow, and take no water yet.
- , ,
- , ,
- , ,
- , ,
- , ,
- , ,
Drill a hole in every fence character that separates two cells watered by the same sprinkler, and leave every other fence character as it is. A + is never a hole.
Number the sprinklers in the order you place them. Going in that order, give each sprinkler the first letter from a to z that keeps rules 2 and 3 against the sprinklers that already carry a letter.