Cyclic Fuel Command Simulation
Problem statement
A vehicle starts at S in a rectangular grid. # is blocked, G is a fuel station, and reaching any cell in the final column succeeds.
Repeat program cyclically for at most maxSteps. Commands U,D,L,R move one cell and consume one fuel. F adds refillAmount fuel and is valid only while on G. Return the first successful step, 0 if already at the goal, or -1 if a command fails or the bound expires.
Function
cyclicProgramSteps(grid: String[], program: String, initialFuel: int, refillAmount: int, maxSteps: int) → intExamples
Example 1
grid = ["S.."]program = "R"initialFuel = 2refillAmount = 3maxSteps = 5return = 2Repeating R reaches the final column in two moves.
Example 2
grid = ["SG.."]program = "RFR"initialFuel = 1refillAmount = 2maxSteps = 6return = 4The vehicle moves onto G, refuels, advances once, then the next cycle reaches the last column.
Example 3
grid = ["S#.","..."]program = "RDL"initialFuel = 5refillAmount = 2maxSteps = 10return = -1The first command attempts to enter an obstacle.
Constraints
1 <= rows, columns <= 200.1 <= program.length <= 10and it contains onlyUDLRF.0 <= initialFuel, refillAmount, maxSteps <= 10^6.- The grid contains exactly one
S.