

1) Cells with a value of 0 indicate empty space in the room.
2) Cells with a value of -1 indicate an obstacle in the room.
3) Cell with a value of 1 indicates the starting position of the robot.
4) Cell with a value of 2 indicates the ending position of the robot.
If ‘N’ = 3, ‘M’ = 3 and ‘ARR’ = [ [1, 0, 0], [0, 0, 0], [0, 0, 2] ]
Then the room is represented as:

The 2 unique paths are marked in the image above.
The first path is: (0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (1, 1) -> (1, 0) -> (2, 0) -> (2, 1) -> (2, 2).
The second path is: (0, 0) -> (1, 0) -> (2, 0) -> (2, 1) -> (1, 1) -> (0, 1) -> (0, 2) -> (1, 2) -> (2, 2).
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains two space-separated integers ‘N’ and ‘M’, denoting the number of rows and columns respectively.
The next N lines each contain M space-separated integers ‘ARR’, denoting each cell of the room.
For each test case, find the number of unique paths using which the robot can clean the room.
Output for each test case will be printed on a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
2 ≤ N, M ≤ 5
ARR[i] = { -1, 0, 1, 2 }
The robot will always have valid starting and ending positions.
Time limit: 1 sec
It’s easy to figure out that we can solve this problem using a standard backtracking practice. We explore the path till the end, while exploring if we ever find any discrepancy on the condition to be satisfied then we revert back to the last change to explore other options of our last stage.
To solve this question we can start from the starting cell of the robot, we can use the input matrix itself to store the cells we have already visited by exploring the current route. We must also store a counter to keep track of the number of cells we have currently visited by exploring the current path. If we are currently at some (i, j) cell then we can try visiting all its neighbouring cells in a depth-first search matter. If at a later stage we get stuck at a place in which we don’t have any adjacent cells that are unvisited then we can simply revert back to the previous cell we came from to explore other routes, note that standard backtracking also involves marking the cell as unvisited when we revert back to the previous cell.
Each time we reach the final cell, we can check if the count of cells in our path is equal to the desired count then we can increment the value of our final answer to be returned.
The steps are as follows :
Maximum Island Size in a Binary Tree
Equal Subtree Sums
Sorted Doubly Linked List to Balanced BST
Longest Substring with K-Repeating Characters
Expression Add Operators