#C6605. Find Words in a Character Matrix
Find Words in a Character Matrix
Find Words in a Character Matrix
You are given a matrix of characters with dimensions \(M \times N\) and a dictionary containing \(K\) words. Your task is to find all the words from the dictionary that can be formed in the matrix. A word can be constructed by connecting adjacent cells in any of the 8 possible directions (up, down, left, right, and the four diagonal directions). Each cell may be used at most once for forming a word.
Note: If no word from the dictionary can be formed, output No words found
.
Example:
Input: 4 4 A B C D E F G H I J K L M N O P 5 ABEF GK CDE MNO NJKL</p>Output: ABEF GK MNO NJKL
The letters in the matrix and the dictionary words are composed of uppercase English alphabets.
inputFormat
The input is read from the standard input (stdin) and has the following format:
- The first line contains two integers \(M\) and \(N\), representing the number of rows and columns of the matrix.
- Next \(M\) lines each contain \(N\) uppercase characters separated by spaces representing the matrix.
- The next line contains an integer \(K\) indicating the number of words in the dictionary.
- The following line contains \(K\) words separated by spaces.
For example:
4 4 A B C D E F G H I J K L M N O P 5 ABEF GK CDE MNO NJKL
outputFormat
Output the words (one per line) from the dictionary that are found in the matrix. The words should be output in the order in which they are found. If no word can be found, output exactly one line containing No words found
.
4 4
A B C D
E F G H
I J K L
M N O P
5
ABEF GK CDE MNO NJKL
ABEF
GK
MNO
NJKL
</p>