
‘U’:- then from (i,j) you can visit (i - 1, j) only if i - 1 >= 0.
‘D’:- then from (i,j) you can visit (i + 1, j) only if i + 1 < n.
‘R’:- then from (i,j) you can visit (i, j + 1) only if j + 1 < m.
‘L’:- then from (i,j) you can visit (i, j - 1) only if j - 1 >= 0.
The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of the test case contains 2 integers ‘N’ and ‘M’ denoting the length and width of the matrix.
The next ‘N’ lines contain ‘M’ characters each denoting the direction of the cell of the matrix.
For each test case, print a single integer denoting a minimum number of cell’s direction need to change to reach from (0,0) to ('N' - 1, 'M' - 1).
Print the output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1<= T <=10
1<= N, M <=100
MATRIX[i][j] = {‘D’,’R’,’U’,’L’}.
Time limit: 1 sec
Explanation:-The main idea is to use a breadth-first search to find minimum changes to reach from the cell (0,0) to all other cells. The cell will be added in the queue if the steps of the current cell is less than the given cell.
The steps are as follows:
Explanation:-The main idea is to use a (0-1) breadth-first search and dequeue to find the minimum distance. We will convert this matrix into a weighted undirected graph of weights 0 and 1.So our problem becomes to find the shortest path from the source (0,0) to the target (m-1,n-1).
The steps are as follows: