Fuel-Aware Grid Reachability
Problem statement
You have a tank whose capacity is fuelCapacity. It starts full. The rectangular grid contains exactly one start cell S, exactly one target cell T, open ground ., blocked cells #, and fuel cells G.
You may move one cell up, down, left, or right. Each move consumes one unit of fuel, so you may leave a cell only when at least one unit remains. You cannot enter a blocked cell. Entering a fuel cell immediately restores the tank to fuelCapacity; a fuel cell may refill the tank again on a later visit.
Return true if you can reach T, and return false otherwise. Reaching T with zero fuel remaining is allowed.
Function
canReachWithFuel(grid: String[], fuelCapacity: int) → booleanExamples
Example 1
grid = ["S..",".#G","...","..T"]fuelCapacity = 4return = trueThe start-to-target distance exceeds four moves, but the path can reach G in three moves, refill to four units, and then reach T.
Example 2
grid = ["S#T","..."]fuelCapacity = 3return = falseThe obstacle forces a four-move detour, and there is no fuel cell to extend the initial three units.
Example 3
grid = ["SGT"]fuelCapacity = 1return = trueThe first move spends the only fuel unit, entering G restores it, and the second move reaches T.
Constraints
1 <= grid.length <= 200.1 <= grid[i].length <= 200, and all rows have the same length.0 <= fuelCapacity <= 400.- Every cell is one of
S,T,.,#, orG. - The grid contains exactly one
Sand exactly oneT.