This time the work mainly uses the word search tree and the depth first search.
1. In the depth-first search, mark the marked array as true before the recursive invocation of the current layer. When a recursive call is returned to the current layer, the marked array should be marked as false. This allows you to not access the nodes on the current node path after the current node has been accessed, but to access the node from other paths.
2. When no word in the dictionary is prefixed with the string that reaches the current node, the depth-first search is stopped.
3. Rewrite the word lookup tree and R to 26 to avoid consuming too much space. In addition, the Keyswithprefix function is changed to the Haskeyswithprefix function because there is no need to find all the words prefixed with a string, but only to determine if there is a word prefixed with a string.
1 ImportJava.util.HashSet;2 ImportJava.util.Set;3 4 Public classBogglesolver5 {6 PrivateTrieSET2 dictionary =NewTrieSET2 ();7 8 Publicbogglesolver (string[] dictionary)9 {Ten for(String s:dictionary) One This. Dictionary.put (s); A } - Private intm, N; - PublicIterable<string>getallvalidwords (boggleboard board) the { -Set<string> set =NewHashset<string>(); -m =board.rows (); -n =board.cols (); + for(inti = 0; I < m; i++) - { + for(intj = 0; J < N; J + +) A { atmarked =New Boolean[m][n]; -DFS (set, board, I, J, "")); - } - } - returnset; - } in Private Boolean[] marked; - Private BooleanIsmarked (intIintj) to { + if(I < 0 | | I >= m | | J < 0 | | J >=N) - return true; the returnMarked[i][j]; * } $ Private voidDFS (set<string> Set, Boggleboard board,intIintJ, String s)Panax Notoginseng { - Charc =Board.getletter (i, j); the if(c = = ' Q ') s + = "QU"; + Elses + =C; A if(!dictionary.haskeyswithprefix (s))return; theMARKED[I][J] =true; + if(S.length () > 2 &&Dictionary.contains (s)) - Set.add (s); $ for(intK =-1; K < 2; k++) $ { - for(intL =-1; L < 2; l++) - { the if(!ismarked (i + K, j +l)) -DFS (set, board, i + K, j +L, s);Wuyi } the } -MARKED[I][J] =false; Wu } - About Public intscoreof (String word) $ { - if(!dictionary.contains (word)) - return0; - intLength =word.length (); A if(Length < 3)return0; + Else if(Length < 5)return1; the Else if(length = = 5)return2; - Else if(length = = 6)return3; $ Else if(length = = 7)return5; the Else return11; the } the}
1 ImportEdu.princeton.cs.algs4.Queue;2 3 Public classTrieSET24 {5 Private Static intR = 26;6 PrivateNode Root;7 8 Private Static classNode9 {Ten Private BooleanHasword; One PrivateNode[] Next =NewNode[r]; A } - - Public voidput (String key) the { -Root = put (root, key, 0); - } - + Private intCharAt (String S,intd) - { + returnS.charat (d)-' A '; A } at - PrivateNode put (node x, String key,intd) - { - if(x = =NULL) x =NewNode (); - if(d = =key.length ()) - { inX.hasword =true; - returnx; to } + intc =charAt (key, D); -X.next[c] = put (x.next[c], key, d+1); the returnx; * } $ Panax Notoginseng Public Booleancontains (String key) - { theNode x = Get (root, key, 0); + if(x = =NULL)return false; A returnX.hasword; the } + - PrivateNode get (node X, String key,intd) $ { $ if(x = =NULL)return NULL; - if(d = = Key.length ())returnx; - intc =charAt (key, D); the returnGet (X.next[c], key, d+1); - }Wuyi the Public BooleanHaskeyswithprefix (String Pre) - { Wu returnGet (root, Pre, 0)! =NULL; - } About}
Coursera algorithm two week 4 boggle