opens. The room in row r and column c opens at time opens[r][c]: you may enter it only at a moment no earlier than that.
At time 0 you stand in the top-left room. Every second you must move to an adjacent room (up, down, left or right); staying put is not allowed. Each move takes exactly one second.
Return the earliest time at which you can be in the bottom-right room, or -1 if it cannot be reached.opens=[[0, 1], [3, 2]] → 2
At time 1 we step right (that room opened at time 1), and at time 2 we step down into the bottom-right room, which opens exactly at time 2.
opens=[[0, 4], [5, 6]] → -1
Both neighbours of the starting room are still closed at time 1, and standing still is not allowed, so there is nowhere to go.
opens=[[0, 1, 1], [9, 9, 6]] → 7
By time 2 we are in the top-right room, but the room below it opens only at time 6. We pace back and forth between the two top-right rooms; because of parity we can step down only at time 7.
opens[r][c] ≤ 10⁵opens[0][0] = 0opens=[[0, 1], [3, 2]]
2
Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.