Shortest Path from Car to Oasis
Problem statement
A rectangular desert grid contains exactly one car C, exactly one oasis O, open ground ., and blocked cells #.
From an open cell, the car may move one cell up, down, left, or right. It may not leave the grid or enter a blocked cell.
Return the minimum number of moves needed to reach the oasis. Return -1 if the oasis is unreachable.
Function
shortestPathToOasis(grid: String[]) → intExamples
Example 1
grid = ["C..",".#.","..O"]return = 4One shortest path uses four orthogonal moves around the blocked center cell.
Example 2
grid = ["C#O","###","..."]return = -1Blocked cells separate the car from the oasis.
Example 3
grid = ["CO"]return = 1The oasis is directly adjacent to the car.
Constraints
1 <= grid.length <= 200.1 <= grid[i].length <= 200, and every row has the same length.- Every cell is one of
C,O,., or#. - The grid contains exactly one
Cand exactly oneO.