Word search - MarisaOJ: Marisa Online Judge

Word search

Time limit: 1000 ms
Memory limit: 256 MB
Given a table with $n$ rows and $m$ columns consisting of uppercase letters, determine whether the word $S$ appears in the table. A word is considered to appear in the table if it can be formed by connecting adjacent cells in the table. Each cell can only be used once. Two cells are considered adjacent if they share an edge. For example, the words `REIMU` and `MARISA` exist in the following table:
### Input - The first line contains two integers $n$ and $m$. - The next $n$ lines each contain a string of length $m$ consisting of uppercase letters, representing the character table. - The last line contains the word $S$ consisting of uppercase letters. ### Output - Print `YES` if the word $S$ exists in the table; otherwise, print `NO`. ### Constraints - $1 \le n,m \le 6$. - $1 \le |S| \le 15$. ### Example Input: ``` 4 4 EIMU RTMA LFTR GASI MARISA ``` Output: ``` YES ```