0-1 BFS (Grid with Some Free Moves)

MEDIUM-HARD · C · 3000ms

For the city's evacuation grid, some intersections are marked as free "shuttle points" (zero-cost to move through) — Lamia needs the minimum-cost evacuation path considering these free shortcuts. Given an N×M grid, moving to an adjacent cell normally costs 1, but moving onto a cell marked 'T' (teleport-friendly) costs 0. Find the minimum cost path from top-left to bottom-right. Use a deque-based 0-1 BFS, not Dijkstra with a heap (unnecessary overhead here).

Input

Line1: N M. Next N lines: grid rows ('.','#' wall,'T').

Output

Minimum cost, or -1 if unreachable.

Constraints

1 ≤ N,M ≤ 500

Sample Input

2 2
. T
# .

Sample Output

0
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

0-1 BFS (Grid with Some Free Moves) · DIU ContestHub