Dsa
Graphs Bfs
Easy
Directions on a Map
Given a map as a grid where each cell indicates the direction of movement ('N', 'S', 'E', 'W'), you start from the top-left cell (0, 0) and must follow the directions to see if you can reach the bottom-right cell (n-1, m-1). If you can reach it, output 'YES', otherwise output 'NO'. You can only move one cell at a time in the direction specified by the current cell.
Input:
The first line contains two integers n and m (1 <= n, m <= 100), the dimensions of the grid.
The next n lines contain m characters ('N', 'S', 'E', 'W').
Output:
'YES' if the bottom-right cell can be reached, 'NO' otherwise.
Example:
Input:
3 3
E E N
S E W
E E S
Output:
YES
Key concepts
graphsbfspathfinding
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