Invasion of the Milkweed
InterviewTime limit1sMemory limit128 MB
Spread milkweed from a start cell to all eight neighbors each week and report the week it covers the last non-boulder cell.
- Level
Easy3 of 10
- Topics
- BFS, Graph, Matrix, Simulation
- Solved
- No attempts yet
Problem
Farmer John has always done his best to keep his pastures full of luscious, delicious, healthy grass for his cows. He has lost this battle, though: the invasive milkweed has taken a foothold in the northwest corner of his farm.
The pasture is partitioned into a rectangular grid of height () and width (). Cell is the lower-left corner, so the grid uses ordinary coordinates. The milkweed starts growing in cell .
Each week the milkweed spreads from every cell it already occupies into all of the (up to eight) surrounding non-boulder cells — the four orthogonal neighbors and the four diagonal neighbors. After spending just one week in a cell, the milkweed is ready to spread out of that cell as well.
Bessie wants to graze for as long as she can before the milkweed takes over, so she wonders how long the pasture will last. If the milkweed occupies cell at week , in which week does it finish covering every non-boulder cell of the pasture? (For every input in this problem, the milkweed always ends up covering the whole pasture.)
The pasture is drawn with . for grass and * for a boulder. For example, with and :
....
..*.
.**.
If the milkweed starts in the lower-left corner (row , column ), the map fills in as shown below. The five snapshots are weeks through from left to right, and M marks a milkweed-covered cell:
.... .... MMM. MMMM MMMM
..*. MM*. MM*. MM*M MM*M
M**. M**. M**. M**. M**M
The milkweed has covered the entire field after weeks.
Input
- Line 1: Four space-separated integers , , , and .
- Lines 2 through : The pasture map, given from the top row down to the bottom row. Line is the top row (row ) and line is the bottom row (row ); each line contains characters,
.for grass and*for a boulder. Because is the lower-left corner, line (for ) describes row .
Output
- Line 1: A single integer: the week number in which the milkweed covers the last remaining non-boulder cell of the pasture.