#K82417. Minimum Time for Field Irrigation

    ID: 35971 Type: Default 1000ms 256MiB

Minimum Time for Field Irrigation

Minimum Time for Field Irrigation

You are given a field represented as an \( N \times M \) grid. Each cell in the grid contains an initial amount of water. A water source is located at cell \( (p_x, p_y) \) (0-indexed). Water flows from the source to all adjacent cells (up, down, left, and right) at a rate of one cell per time unit.

The goal is to determine the minimum number of time units required so that every cell in the grid has at least \( W \) units of water. Note that if a cell already has at least \( W \) units of water at the beginning, it does not contribute to the time required. In other words, the answer is the maximum shortest distance from the water source to any cell whose water amount is below \( W \).

inputFormat

The input is given via standard input (stdin) with the following format:

N M
row1_element1 row1_element2 ... row1_elementM
...
rowN_element1 rowN_element2 ... rowN_elementM
p_x p_y
W

Where:

  • \( N \) and \( M \) denote the number of rows and columns of the grid respectively.
  • The next \( N \) lines each contain \( M \) space-separated integers representing the initial water in each cell.
  • \( p_x \) and \( p_y \) indicate the coordinates of the water source (0-indexed).
  • \( W \) is the required minimum water units for each cell.
  • outputFormat

    Output a single integer to standard output (stdout): the minimum time (in time units) required such that every cell in the grid has at least \( W \) units of water.

    ## sample
    1 1
    0
    0 0
    1
    
    0