KatInterview

Firebreak Choice in a Spreading Crop Disease Grid

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingsimulationgridbrute-force

Last updated

Simulate a synchronous grid disease model with delayed death or recovery, then try every single-row or single-column firebreak to minimize dead plants.

A field is an R x C grid of plants. Each plant starts in one of four conditions, given as lowercase strings: "healthy", "infected", "recovered" or "dead". Two plants are neighbors if they share an edge, so each plant has at most four neighbors.

Daily rules

The simulation runs for days days, numbered 1, 2, .... On each day, every plant that is not dead and not already waiting on an outcome (see below) is examined. Let m be the number of its neighbors that are currently infected, using the grid as it was at the start of that day.

  • If m >= high, the plant is doomed: it will become dead.
  • Otherwise, if m >= low, the plant is recovering: it will become recovered.
  • Otherwise, nothing happens to it.

A doomed or recovering plant is waiting. It is treated as infected for neighbor counts the whole time it waits. It is not examined again until its outcome is applied.

Timing of the outcome

A plant that starts waiting on day d has its outcome applied at the end of day d + delay - 1. For delay = 1, the outcome is applied at the end of day d itself, so the plant never appears as waiting at the start of a later day. For delay = 2, the plant waits through day d + 1 and changes at the end of that day. Outcomes that would land after day days are never applied, so those plants are not dead.

Simultaneity

All decisions on a day use only the snapshot from the start of that day. All changes for that day (new waiting plants and applied outcomes) are written together at the end of the day.

Other details

  • Dead plants never change and never count as infected.
  • Plants that are "infected" at the start and not waiting are examined like any other non-dead plant. They can start waiting on any day.
  • Recovered plants are examined too, so they can become infected again and start waiting.
  • A plant is counted as infected if it is in the "infected" condition or is waiting. Healthy and recovered plants are not infected.
  • With low = 0, every examined plant starts waiting on every day it is examined. Apply the rules literally.

Firebreak choice

Before day 1, you must burn exactly one whole row or exactly one whole column. Every plant on that line immediately becomes dead, even if it was already dead.

Try all R + C options. For each, simulate days days and count the dead plants in the final grid, including the burned ones and plants that were dead from the start. Return the smallest count together with the line that achieves it.

Tie-break: Check rows in increasing index order, then columns in increasing index order. Report the first option that reaches the minimum, so a later option replaces it only if strictly smaller.

Function

Return a list [min_dead, [kind, index]], where kind is the string "row" or "column" and index is the 0-based index of the burned line.

Constraints

  • 1 <= R, C, and the grid is rectangular.
  • 0 <= low <= high
  • delay >= 1
  • days >= 0
  • Every cell is one of "healthy", "infected", "recovered", "dead".
  • days may be large relative to the grid, so stopping early once the grid stops changing is worthwhile.

Examples

Example 1

Input: min_dead_after_burn([['infected', 'healthy', 'healthy'], ['healthy', 'healthy', 'healthy']], 1, 3, 1, 2)

Output: [2, ['column', 0]]

Example 2

Input: min_dead_after_burn([['healthy', 'healthy', 'healthy'], ['healthy', 'infected', 'healthy'], ['healthy', 'healthy', 'healthy']], 1, 4, 2, 3)

Output: [3, ['row', 0]]

Example 3

Input: min_dead_after_burn([['infected', 'healthy', 'recovered', 'infected']], 1, 2, 3, 5)

Output: [1, ['column', 0]]

Hints

Hint 1

Write a function that simulates one fixed starting grid for `days` days and returns the dead count, then call it once for every row and every column.

Hint 2

Keep a numeric state per cell plus a separate waiting counter, and build brand-new arrays each day so every decision sees only the start-of-day snapshot. Waiting plants must count as infected.

Hint 3

Work out the timing on paper: with `delay = 1` the outcome is applied the same day, so a waiting plant is stored with counter `delay - 1`. If a full day leaves both the state and the counters unchanged, the simulation has reached a fixed point and you can stop.