Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must is constructed from letters of sequentially adjacent cell, where "adjacent" cells is those horizontally or Vertically neighboring. The same letter cell is used more than once in a word.
For example,
Given words = and ["oath","pea","eat","rain"]
board =
[ [' O ', ' a ', ' a ', ' n '], [' e ', ' t ', ' a ', ' e '], [' I ', ' h ', ' K ', ' R '], [' I ', ' f ', ' l ', ' V ']]
Return ["eat","oath"]
.
class Solution {public: vector<string> findwords (vector<vector<Char >>& Board, vector<string>& words) { /* just saw a word before the topic thought Search, so immediately think of the violence of searching every word, but unfortunately with the same as expected time-out, looked at hint, referred to the dictionary tree. So first use the words given to build the dictionary tree, and then in the DFS search the dictionary tree of words, so as to avoid a large number of repeated comparisons. In addition, if the search path is not a dictionary tree prefix, you can directly prune back. The following is the AC code. Because to avoid repetition, I first use a set to save the result and then dump it into result. */ }};
[Leedcode 212] Word Search II