

You are given an array of strings “arr” and a character matrix “mat”. Your task is to find out all the possible words in the array “arr” that can be constructed by moving sequentially in the matrix “mat”, where the movement can start from any position in the matrix “mat” and can be done in all 8 possible directions. However, the same cell can not be considered more than once for the construction of a word. Print the words in sorted manner.
For Example:If arr = {“go”, “pp”, “no”, “op”}, mat = {{‘g’,’p’},
{‘d’,’o’}}.
Here, the two-letter words that can be formed from the matrix “mat” are “gp”, “gd”, “go”, “pg”, “pd”, “po”, “dg”, “dp”, “do”, “og”, “op”, “od”.
So, the words that are present in the array “arr” which can be formed from the matrix “mat” are “go” and “op”.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case contains an integer P, denotes the length of the array.
The second line of each test case contains, P space separated strings.
The third line of each test case contains two space separated integers N, and M, denoting the number of rows and columns in a 'mat”.
The next N lines contain M characters i.e. the elements of the matrix “mat”.
Output format:
For each test case, print all the possible words in the array “arr” in a sorted manner, that can be constructed by moving sequentially in the matrix “mat”.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= P <= 10
1 <= arr[i].length <= 50
1 <= N, M <= 7
All the characters of the strings in the array “arr” and matrix “mat” contain lowercase English letters only.
All the words in the array “arr” are unique.
Time limit: 1 second
1
6
go on no of pon pgp
2 2
g n
o p
go no on pon
For the first test case, the word “go” can be formed by (0,0) → (1,0).
The word “on” can be formed by (1,0) → (0,1).
The word “no” can be formed by (0,1) → (1,0).
The word “of” can never be formed as there is no ‘f’ character in the matrix.
The word “pon” can be formed by (1,1) → (1,0) → (0,1).
The word “pgp” can not be formed as there is only one ‘p’ character in the matrix.
1
8
person so warm on code no son rn
2 3
p e r
n o s
no on person so son
For the first test case, the word “person” can be formed by (0,0) → (0,1) → (0,2) → (1,2) → (1,1) → (1,0).
The word “so” can be formed by (1,2) → (1,1).
The word “warm” can never be formed as there is no character ‘w’ in the matrix.
The word “on” can be formed by (1,1) → (1,0).
The word “code” can never be formed as there is no character ‘c’ in the matrix.
The word “no” can be formed by (1,0) → (1,1).
The word “son” can be formed by (1,2) → (1,1) → (1,0).
The word “rn” can not be formed as the character ‘r’ and ‘n’ are not adjacent to each other.
Think of a brute-force solution using backtracking.
The idea here is to start the search from every cell of the matrix mat and find all possible words that can be formed starting from this character using Backtracking. While doing so, we also keep a visited matrix and each time we visit a cell, we mark this cell as visited in the visited matrix, so that we do not consider the same character again and again while doing the traversal.
Steps:
void possibleWordsUtil(mat, arr, visited, i, j, n, m, curr, st):
O(N*M * (8 ^ (N*M))), where N and M are the number of rows and columns in the matrix “mat” respectively.
In the worst case, for each cell in the matrix mat, we will be traversing in 8 possible directions in the whole matrix. So, for each cell, it takes 8 ^ (N * M) time and as there are (N ^ M) cells in the matrix, hence, the overall time complexity becomes O(N*M * (8 ^ (N*M))).
O(N * M), where N and M are the number of rows and columns in the matrix “mat” respectively.
In the worst case, extra space is required for recursive calls, and also we are using a visited matrix of size (N * M) to keep track of the visited cells. Hence, the space complexity will be O(N * M).