*LeetCode--79.Word Search 212. Word Search II(类似迷宫问题)
Word Search: http://blog.csdn.net/ljiabin/article/details/45846231
Word Search II: http://blog.csdn.net/ljiabin/article/details/45846527
Word Search
public boolean exist(char[][] board, String word) { int row = board.length; if(row == 0)return word.isEmpty(); int col = board[0].length; if(col == 0) return word.isEmpty(); boolean[][] visited = new boolean[row][col]; for(int i = 0 ; i < row ; i++){ for(int j = 0 ; j < col ; j++){ //从每一个元素开始进行深搜,只要找到一个可行方案即可返回true if(dfs(board,visited,i,j,word,0)) return true; } } return false; } //采用深搜 public boolean dfs(char[][] board, boolean[][] visited, int x, int y,String word, int k) { //如果已经匹配到最后了,true if (k == word.length()) return true; //如果有一个下标过界了,false if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) return false; //如果当前元素已经访问,false if (visited[x][y]) return false; //如果当前元素和要匹配的字母不相等,false if (board[x][y] != word.charAt(k)) return false; //如果四个方向有一个可以递归访问并且得到结果,返回true visited[x][y] = true; if (dfs(board, visited, x - 1, y, word, k + 1)) return true; if (dfs(board, visited, x + 1, y, word, k + 1)) return true; if (dfs(board, visited, x, y - 1, word, k + 1)) return true; if (dfs(board, visited, x, y + 1, word, k + 1)) return true; //若四个方向都得不到结果,当前设置为未访问,返回false visited[x][y] = false; return false; }
Word Search II
public class Solution { public static void main(String[] args) { // TODO Auto-generated method stub char[][] board = { {'o','a','a','n'}, {'e','t','a','e'}, {'i','h','k','r'}, {'i','f','l','v'} }; String[] words = {"oath","pea","eat","rain"}; System.out.println(findWords(board,words)); } public static List<String> findWords(char[][] board, String[] words) { HashSet<String> solu = new HashSet<String>(); Trie trie = new Trie(); for(String w : words){ trie.insert(w); //将当前单词加入到字典树中 } int row = board.length; int col = board[0].length; boolean[][] visited = new boolean[row][col]; for(int i = 0 ; i < row ; i++){ for(int j = 0 ; j < col ; j++){ dfs(board,visited,i,j,"",trie,solu); } } return new ArrayList<String>(solu); } //采用深搜 public static void dfs(char[][] board, boolean[][] visited, int x, int y, String word, Trie trie ,HashSet<String> solu) { if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) return ; if (visited[x][y]) return ; word += board[x][y]; if(!trie.startsWith(word)) return; if(trie.search(word)==true) //如果找到了单词 solu.add(word); visited[x][y] = true; dfs(board, visited, x - 1, y, word, trie, solu); dfs(board, visited, x + 1, y, word, trie, solu); dfs(board, visited, x, y - 1, word, trie, solu); dfs(board, visited, x, y + 1, word, trie, solu); visited[x][y] = false; } }
字典树的实现:
//利用字典树 class TrieNode { // Initialize your data structure here. char c ; TrieNode[] branch; public TrieNode() { branch = new TrieNode[26]; } } class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } // Inserts a word into the trie. public void insert(String word) { int len = word.length(); TrieNode curr = root; char c; for(int i = 0 ;i < len ; i++){ c = word.charAt(i); if(curr.branch[c-97]==null) curr.branch[c-97] = new TrieNode(); curr = curr.branch[c-97]; } curr.c = '*'; } // Returns if the word is in the trie. public boolean search(String word) { int len = word.length(); TrieNode curr = root; char c; for(int i = 0 ; i < len ; i++){ c = word.charAt(i); if(curr.branch[c-97]==null) return false; curr = curr.branch[c-97]; } if(curr.c!='*') return false; return true; } // Returns if there is any word in the trie // that starts with the given prefix. public boolean startsWith(String prefix) { int len = prefix.length(); TrieNode curr = root; char c; for(int i = 0 ; i < len ; i++){ c = prefix.charAt(i); if(curr.branch[c-97]==null) return false; curr = curr.branch[c-97]; } return true; } } // Your Trie object will be instantiated and called as such: // Trie trie = new Trie(); // trie.insert("somestring"); // trie.search("key");

浙公网安备 33010602011771号