A grid contains a start S, a destination D, blocked cells, and cells labeled by transit mode. A trip must use one mode for its entire route. Choose the reachable mode with the best travel time and cost.
Question
A grid contains a start `S`, a destination `D`, blocked cells, and cells labeled by transit mode. A trip must use one mode for its entire route. Choose the reachable mode with the best travel time and cost.
```python
def best_mode(grid: list, modes: list, times: list, costs: list) -> str: ...
```
## Requirements
- Digit `'1'` identifies `modes[0]`, digit `'2'` identifies `modes[1]`, and so on.
- Movement is allowed in four cardinal directions through cells of the chosen digit.
- `S` and `D` may be entered for free and add neither time nor cost.
- `X`, unrelated mode digits, and any other symbols are blocked.
- For a route using mode `i`, each mode cell contributes `times[i]` and `costs[i]`.
- Choose minimum total time, then minimum total cost, then earlier position in `modes`. Return `""` if no mode reaches `D`.
Assume a nonempty rectangular grid with exactly one `S` and one `D`, at most nine modes, equal-length mode/time/cost arrays and nonnegative per-cell costs. Adjacent `S` and `D` therefore yield zero totals for every mode, and the first mode wins. `_mode_cost` is an optional internal helper, not a separately assessed interface.
## Design discussion (not graded)
1. Return the selected route as well as the mode.
2. Permit transfers with a transfer penalty.
3. Allow time-dependent congestion or limited-capacity edges.
4. Answer repeated queries on the same map.
The public brief is identical for people and crawlers. Account access changes tools, not the indexed description.