Repair One Movement Instruction
Problem statement
You are given a rectangular character grid and a movement program.
Sis the unique start cell.Tis the unique target cell.#is an obstacle..is an open cell.
The robot starts on S facing up. Each character of instructions is one command:
F: move one cell forward.B: move one cell backward without changing direction.L: turn left in place.R: turn right in place.
You must replace exactly one instruction with a different command from F, B, L, and R. A candidate replacement is invalid if any move leaves the grid or enters an obstacle.
Return true if some valid replacement makes the robot finish on T after executing every instruction. Its final facing direction does not matter.
Function
canReachTargetWithOneReplacement(grid: String[], instructions: String) → booleanExamples
Example 1
grid = [".T.","...",".S."]instructions = "FR"return = trueReplace R with F. The robot moves up twice and finishes on T.
Example 2
grid = ["T#","S."]instructions = "F"return = falseThe original instruction reaches T, but it must be replaced. Each different command either turns in place or leaves the grid.
Example 3
grid = ["..T",".#.","S.."]instructions = "FLRFF"return = trueReplace the second command L with F. The repaired program FFRFF travels around the obstacle.
Constraints
1 ≤ grid.length, grid[r].length ≤ 100- Every row has the same length.
- The grid contains exactly one
Sand oneT. - Every other cell is
.or#. 1 ≤ instructions.length ≤ 2000instructionscontains onlyF,B,L, andR.