Loading...
Loading...
Loading...
A letter grid has R rows and C columns. A word can be read by starting at any cell and moving up, down, left or right to a neighbouring cell, never using the same cell twice in one word. Given n words, print those that can be read in the grid, in alphabetical order, one per line. Print NONE if there are none.
Example 1
Input
3 4 oaan etae ihkr 4 oath pea eat rain
Output
eat oath
oath and eat can be read. pea and rain cannot.
Example 2
Input
1 1 a 1 a
Output
a
Try the problem first. Open one hint at a time.
Put all words in a trie and start a depth-first search from every cell.
Mark the cell as used while you explore from it, and unmark it when you come back.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor