
In a particular sentence, the robot can use a single word from the row that the word is part of.
The first line of input contains an integer ‘T’ denoting the number of test cases.
Then the T test cases follow.
The first line of each test case contains two integers ‘N’ and ‘M’ , the rows and columns of the 2D array that contains the list of words.
The next ‘N’ lines contain ‘M’ strings each.
For each test case, all the possible sentences formed are printed in a separate line.
The output of each test case is printed in a separate line.
The output format is predefined for this question. You need to store all possibly created sentences in a predefined data structure. Keep in mind that first you need to store all the sentences that start with the first word of the first row and so on.
For further clarification on the same see the below example:
1
2 2
you we
sleep drink
In this case there are more than one correct ways to store the sentences but order of the output matters so, you should keep in mind that you start with the first row and the first column and so on.
Correct Output:
you sleep
you drink
we sleep
we drink
Incorrect outputs:
we sleep we drink you drink
we drink we sleep you sleep
you sleep you drink we drink
you drink you sleep we sleep
1 <= T <= 5
1 <= N <= 5
1 <= M <= 5
1 <= W <=20
where ‘T’ is the number of test cases, ‘N’ and ‘M’ are the dimensions of the words array, ‘array[i][j]’ is the value of words of the array, and ‘W’ denotes the size of the word.
We will use the basic idea of depth-first traversal in order to solve this problem. We create an output array to store the created sentence. We now start by considering all the words of the first row as the starting points for sentences and store them in the output array.
Let us look at the below example:
Arr = you we
have are
sleep read
Steps: