예제 #1
0
        private List <Word> GetWordsTree()
        {
            var wordsUsed  = new List <string>();
            var returnList = new List <Word>();
            var wordQueue  = new Queue <Word>();
            var dictionary = _dictionary.GetDictionary().Where(d => d.Length == _firstWord.Length);

            wordQueue.Enqueue(new Word {
                Text = _firstWord, Parent = null, Position = 1
            });
            returnList.Add(new Word {
                Text = _firstWord, Parent = null, Position = 1
            });

            while (wordQueue.Count > 0)
            {
                var currentWord = wordQueue.Dequeue();

                foreach (var word in dictionary)
                {
                    if (wordsUsed.Contains(word))
                    {
                        continue;
                    }
                    ;

                    if (IsChildNode(currentWord.Text, word))
                    {
                        var childWord = new Word {
                            Text = word, Parent = currentWord.Text, Position = currentWord.Position + 1
                        };
                        wordQueue.Enqueue(childWord);
                        returnList.Add(childWord);
                        wordsUsed.Add(word);

                        if (word == _lastWord)
                        {
                            return(returnList);
                        }
                    }
                }
            }
            ;

            return(null);
        }