Dsa
Backtracking
Hard
Treasure Island Paths
You are on a treasure hunt on a grid where certain cells contain obstacles. You can only move right or down to find the treasure located at the bottom-right corner. Print all unique paths from the top-left corner to the treasure while avoiding obstacles.
Input Format:
The first line contains two integers n and m (1 ≤ n, m ≤ 10) representing the dimensions of the grid.
The next n lines contain m characters each ('.' for open cells, '#' for obstacles).
Output Format:
Print each valid path as a sequence of moves (D for down, R for right) on a new line.
Example:
Input:
3 3
...
.#.
...
Output:
DDR
DRD
RDDR
RDR
Key concepts
backtrackinggrid navigationcombinatorics
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now