Cave Advanture
Learn this problemProblem statement
A cave is represented by a rectangular map of N rows and M columns. A period . marks a safe chamber, while X marks an unsafe chamber that cannot be entered.
The expedition starts at [0,0] and must reach the exit at [N-1,M-1]. From [r,c], explorers may move to [r,c-1], [r,c+1], or [r+1,c]. They cannot move upward, leave the map, enter an unsafe chamber, or revisit a chamber.
Return the maximum number of distinct safe chambers that can be visited along a valid route from the entrance to the exit. Return -1 when the exit cannot be reached.
Function
allAboutCave(A: String[]) → intExamples
Example 1
A = ["..X.","...X","....","X..."]return = 9A longest valid route starts at the top-left chamber, moves only left, right, or down without revisiting a chamber, reaches the bottom-right exit, and visits 9 safe chambers.
Example 2
A = [".X...",".X...","...X."]return = -1The safe regions cannot be connected from the entrance to the exit without moving upward or crossing an unsafe chamber, so the exit is unreachable.
Example 3
A = ["......",".XXXX.","...X..","...X.X","......"]return = 14The longest valid route reaches the exit while visiting 14 distinct safe chambers. It never moves upward or re-enters a chamber.
More Meta problems
- Plan a Minimum-Cost Round TripONSITE INTERVIEW · Seen Jul 2026
- Merge Three Sorted ArraysPHONE SCREEN · Seen May 2026
- Highest Rating Price RatioOA · Seen Mar 2026
- Diagonal Traverse (for E4 ;)PHONE SCREEN · Seen Mar 2025
- Find Peak ElementPHONE SCREEN · Seen Mar 2025
- Find Pair Closest to K (for E5 :)PHONE SCREEN · Seen Feb 2025
- Get Minimum Round Trip Cost (: for E4 && E5 :)PHONE SCREEN · Seen Feb 2025
- Max Consecutive Ones III (for E5 :)PHONE SCREEN · Seen Feb 2025