zoukankan      html  css  js  c++  java
  • 212. Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board.

    Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

    For example,
    Given words = ["oath","pea","eat","rain"] and board =

    [
      ['o','a','a','n'],
      ['e','t','a','e'],
      ['i','h','k','r'],
      ['i','f','l','v']
    ]
    

    Return ["eat","oath"].

    Note:
    You may assume that all inputs are consist of lowercase letters a-z.

    public class Solution {
        public IList<string> FindWords(char[,] board, string[] words) {
            var res = new List<string>();
            if(words.Count() == 0 || board == null) return res;
            Trie trie = new Trie();
            foreach(string word in words)
            {
                trie.Insert(word);
            }
            var visited = new bool[board.GetLength(0),board.GetLength(1)];
            for(int i = 0;i<board.GetLength(0) ;i++)
            {
                for(int j = 0;j<board.GetLength(1) ;j++)
                {
                    if(trie.Root.Child[board[i,j]-'a'] != null)
                    {
                        Search(board,trie.Root.Child[board[i,j]-'a'], visited,i,j,res,board[i,j]+"");
                    }
                }
            }
            return res;
        }
        
        
        private void Search(char[,] board,TrieNode t, bool[,] visited, int row, int col,IList<string> res, string cur)
        {
            if(t != null && t.EndWord)
            {
                if(!res.Contains(cur))res.Add(cur);
            }
            if(row<0 || row >= board.GetLength(0)|| col<0 || col >= board.GetLength(1))
            {
                return;
            }
            else
            {
                visited[row,col] = true;
                if(row>0 && !visited[row-1,col] && t.Child[board[row-1,col]-'a'] != null ) Search(board,t.Child[board[row-1,col]-'a'], visited, row-1, col,res, cur+board[row-1,col]);
                if(row<board.GetLength(0)-1 && !visited[row+1,col] && t.Child[board[row+1,col]-'a'] != null) Search(board,t.Child[board[row+1,col]-'a'], visited, row+1, col,res, cur+board[row+1,col]);
                if(col>0 &&!visited[row,col-1] && t.Child[board[row,col-1]-'a'] != null ) Search(board,t.Child[board[row,col-1]-'a'], visited, row, col-1,res, cur+board[row,col-1]);
                if(col<board.GetLength(1)-1 && !visited[row,col+1] && t.Child[board[row,col+1]-'a'] != null) Search(board,t.Child[board[row,col+1]-'a'], visited, row, col+1,res, cur+board[row,col+1]);
                 visited[row,col] = false;
                
            }
        }
    }
    
    public class TrieNode {
        public TrieNode[] Child {get;set;}
        public bool EndWord {get;set;}
        public TrieNode()
        {
            Child = new TrieNode[26];
            EndWord = false;
        }
        
    }
    public class Trie{
        public TrieNode Root {get;set;}
        
        public Trie()
        {
            Root = new TrieNode();
        }
        
        public void Insert(string word)
        {
            var sentinel = Root;
            foreach(char c in word)
            {
                if(sentinel.Child[c-'a'] == null) sentinel.Child[c-'a'] = new TrieNode();
                sentinel = sentinel.Child[c-'a'];
            }
            sentinel.EndWord = true;
        }
    }
  • 相关阅读:
    高级语言是面向用户的
    汇编语言指令是机器指令的符号化
    程序设计语言具有心理工程及技术
    语言的种类成分
    程序设计方法和过程
    程序设计的基本概念
    结构化程序设计与非结构化程序设计之分
    常见语言计算机语言
    语言的基础是一组记号和一组规则
    面向对象的基本概念
  • 原文地址:https://www.cnblogs.com/renyualbert/p/5894746.html
Copyright © 2011-2022 走看看