FastPrepFastPrep
Problem Brief

Grid Infection Spread Until Stable

FULLTIMEPHONE SCREEN

Complete the function below. The function receives the full standard input as a single string and must return the exact standard output lines for the described problem.

Problem

Grid Infection Spread Until Stable (8-direction, threshold)

You are given an m×n grid grid where each cell is either infected 'X' or healthy '.'.

Each day, perform a synchronous update: for every healthy cell '.', count the number of infected 'X' cells among its 8 neighbors (4-directional + 4 diagonals). If the count is ≥ T, that cell becomes infected 'X' at the end of the day. Infected cells never recover or disappear.

Return the number of days until the grid becomes stable (i.e., after some day’s update, no new infections occur).

Notes:

Updates are synchronous: changes on day d depend only on the state at the start of day d.

You may assume 1 ≤ m,n ≤ 200 and 0 ≤ T ≤ 8.

Examples:

grid=["...", ".X.", "..."], T=1 → 1

same grid, T=2 → 0

Example

Input

3 3

1

...

.X.

...

Output

1

Function Description

Complete solveGridInfectionSpreadUntilStable. It has one parameter, String input, containing the full stdin payload. Return the stdout payload as an array of lines, without trailing newline characters.

1Example 1

Input
input = "3 3\n1\n...\n.X.\n..."
Output
["1"]
Explanation

The returned string array must match the expected standard output lines for the sample input.

Constraints

Limits and guarantees your solution can rely on.

Use the limits and requirements stated in the prompt.

public String[] solveGridInfectionSpreadUntilStable(String input) {
    // write your code here
}
Input

input

"3 3\n1\n...\n.X.\n..."

Output

["1"]

Sign in to submit your solution.