Minimum Trainer Battles
You are navigating a 2D grid forest to reach an exit. The grid contains:
- 'S': Your start (exactly one).
- 'E': The exit (exactly one).
- 'T': An impassable tree.
- '0' to '9': Empty cells with that many rival trainers.
In one turn, you and all rivals can move 1 step up, down, left, or right to any non-tree cell. If you are on 'E', you can leave the forest.
Because you publish your exact path to the exit beforehand, all rivals know it. They will move optimally to intercept and battle you. If you and any rivals occupy the same cell at the same time, a battle occurs and those rivals immediately leave. Rivals who cannot possibly intercept you do nothing.
Return the minimum number of battles you must fight to safely leave the forest, assuming you choose the optimal path.
Example 1:
Input: grid = [["S","E","2","3"]]
Output: 2
Explanation:
You step right to 'E'. In the same turn, the 2 trainers step left to 'E' and intercept you. The 3 trainers are too far behind. You fight 2 battles.
Example 2:
Input: grid = [["S", "0", "E", "T", "9"]]
Output: 0
Explanation:
You walk to the exit. The 9 trainers are completely blocked by a tree ('T') and can never reach you or the exit. You fight 0 battles.
Now Your Turn!
Pick the correct output for the given inputInput: grid = [
["S", "0", "0", "E"],
["T", "T", "T", "0"],
["9", "0", "0", "0"]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= grid.length <= 1000
- 1 <= grid[i].length <= 1000
- grid[i][j] is 'S', 'E', 'T', or a digit '0'-'9'.
- It is guaranteed that a valid path exists from 'S' to 'E'.