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.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)).
field=[[0, 1], [0, 0]] → 1
Both (0, 0) and (1, 1) are right next to the sentry, so nothing beats 1.
field=[[1]] → 0
Start and finish are the same cell, and a sentry stands on it: the answer is 0.
field is an n × n grid of 0s and 1sfield=[[0, 0, 0], [0, 0, 0], [1, 0, 0]]
2
Run checks the first 3 tests in your browser. Submit checks all 68 tests on the server, hidden ones included.