Ctrl K

Steer Clear of the Sentries

Medium
You are given a square n × n grid field: 1 marks a cell with a sentry, 0 an empty cell. You need to walk from the top-left cell (0, 0) to the bottom-right cell (n - 1, n - 1), stepping between cells that share a side. Any cell may be stepped on, sentry cells included. The distance between (r1, c1) and (r2, c2) is |r1 - r2| + |c1 - c2|. The **safety** of a route is the smallest distance from any cell on the route to any sentry. Return the highest safety a route can achieve.
Example 1
field=[[0, 0, 0], [0, 0, 0], [1, 0, 0]] → 2

The sentry sits in the bottom-left corner. Going along the top row and down the right column never gets closer than 2 to it (the closest cells are (0, 0) and (2, 2)).

Example 2
field=[[0, 1], [0, 0]] → 1

Both (0, 0) and (1, 1) are right next to the sentry, so nothing beats 1.

Example 3
field=[[1]] → 0

Start and finish are the same cell, and a sentry stands on it: the answer is 0.

Constraints
  • 1 ≤ n ≤ 400
  • field is an n × n grid of 0s and 1s
  • There is at least one sentry
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRoute With the Highest Bottleneck MediumThe Gentlest Climb MediumCrossing at High Tide HardLast Dry Crossing Day HardHow Far the Water Spreads Hard
loading Python…
Input
field=[[0, 0, 0], [0, 0, 0], [1, 0, 0]]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 68 tests on the server, hidden ones included.