Esempio n. 1
0
        public Result GetMaxWordsRectangle(List <string> words)
        {
            Result   result   = null;
            WordsMap wordsMap = new WordsMap(words);

            int     rowLength;
            Indices currentIndices = new Indices();
            Trie    columnWords;

            while (wordsMap.Get(currentIndices.RowLengthIndex, out rowLength, out columnWords))
            {
                Indices currentInnerIndices = new Indices();
                currentInnerIndices.RowLengthIndex = currentIndices.RowLengthIndex;

                int  columnLength;
                Trie rowWords;
                while (wordsMap.Get(currentInnerIndices.ColumnLengthIndex, out columnLength, out rowWords))
                {
                    WordsMatrix matrix = GetMaxWordsRectangle(wordsMap, rowWords, columnWords, rowLength, columnLength);
                    if (matrix != null)
                    {
                        //Return first result since we start with max length
                        return(new Result(matrix.Size, matrix.GetList()));
                    }

                    currentInnerIndices.IncrementColumn();
                }

                currentIndices.IncrementRow();
            }

            return(result);
        }
Esempio n. 2
0
        private WordsMatrix GetMaxWordsRectangle(WordsMap wordsMap, Trie rowWords, Trie columnWords, int rowLength, int columnLength)
        {
            foreach (string rowWord in rowWords)
            {
                WordsMatrix wordsRectangle = new WordsMatrix(rowLength, columnLength);
                // Alternate betweem row and column to fill the rectangle
                FillRowInRectangle(wordsRectangle, new WordsMatrix.Location(), new WordsMatrix.Location(),
                                   rowWords, columnWords, rowWord);
                // When returned from recursive calls check the last column
                if (wordsRectangle.IsFull)
                {
                    return(wordsRectangle);
                }
            }

            return(null);
        }