function board = solver(words, weights, n, penalty)
% Crossword Solver
% WordList02 -- all rows
% results: 6991690.00
% time: 0.27
% WordList01 -- alternate rows
% results: 7494085.00
% time: 0.11
board = zeros(n);
[~,WordLen] = cellfun(@size,words); % Length of each word
WordPriority = weights ./ (WordLen+1); % Guess at priority
[WordPriority,Index] = sort(WordPriority,'descend'); % Find words in priority order
WordLen = WordLen(Index);
words = words(Index); % ... and sort words to match
Used = false(size(WordLen));
Next = 1;
for Row = 1:1:n % Start by filling alternate rows
Col = 1;
while Col < n
if n-Col+1 >= WordLen(Next) % Check!
board(Row,Col:Col+WordLen(Next)-1) = words{Next};
Used(Next) = true; % Not currently used
Next = Next+1;
Col = Col+WordLen(Next)+1;
else
Col = n+1;
% For now ignore possibility of fitting in short words
% at end
end
end
end
end
|