Dsa
Backtracking
Easy
Treasure Hunt
You are on a treasure hunt in a grid. The grid consists of '.' representing walkable paths and '#' representing walls. You are given the starting position (x, y) and the destination position (a, b). Your task is to determine if it is possible to reach the destination from the starting position by moving up, down, left, or right. You can only move on '.' cells. If it’s possible, print 'YES', otherwise print 'NO'.
Input Format:
The first line contains two integers, n (rows) and m (columns).
The next n lines contain m characters each ('.' or '#').
The last line contains four integers x, y (starting position) and a, b (destination position). (0-indexed)
Output Format:
Print 'YES' if the destination is reachable, otherwise print 'NO'.
Example Input:
3 3
...
.#.
...
0 0 2 2
Example Output:
YES
Key concepts
backtrackingsearchcombinatorial
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