Minimum Direction Changes Problem Statement

Given a 2D grid with 'N' rows and 'M' columns, where each cell contains a character from the set { 'U', 'L', 'D', 'R' } indicating the allowed direction to move to a neighboring cell, determine the minimum number of cells that need to be changed to create a valid path from the top-left to the bottom-right corner of the grid.

Input:

 T 
N M
grid[0][0] grid[0][1] ... grid[0][M-1]
...
grid[N-1][0] grid[N-1][1] ... grid[N-1][M-1]

Output:

 Minimum number of direction changes to create the path for each test case. 

Example:

Input:
1 
2 2
R D
L D
Output:
0
Explanation:

The given grid allows a direct path from top-left to bottom-right following the directions without any changes.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N, M ≤ 100
  • Each cell contains one of {'U', 'L', 'D', 'R'}.
  • Time Limit: 1 second

Note:

You do not need to handle input/output. Implement the function and return the result.

Be the first one to answer
Add answer anonymously...
Optum Global Solutions Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter