Finish 2011-04-20 12:00:00 UTC

vertical 2

by lcome

Status: Passed
Results: 8438771 (cyc: 7, node: 238)
CPU Time: 13.215
Score: 21097.1
Submitted at: 2011-04-13 23:06:40 UTC
Scored at: 2011-04-13 23:12:32 UTC

Current Rank: 1517th (Highest: 15th )
Based on: vertical 1 (diff)
Basis for: vertical 4 (staggering) (diff)

Comments
Please login or create a profile.
Code
function board = solver(words, weights, n, penalty)

board = zeros(n);
Used  = false(n+2);

NbWords = length(words);
for w = NbWords:-1:1
    WordsLength(w) = length(words{w});
end

RelativeWeights = weights ./ (WordsLength + 1);
[~,ind] = sort(RelativeWeights,'descend');

for w = 1:NbWords
    Word_Length = WordsLength(ind(w));
    
    % vertical placement :
    Zone = false(n+2);
    Zone(2:2+n-Word_Length , 2:end-1) = true;
    PossiblePosition = Zone & ~Used;
    [ROW,COL] = find(PossiblePosition); % start at (2,2)
    for pos = 1:length(ROW)
        row = ROW(pos);
        col = COL(pos);
            if      all(~Used(row + ( 0:Word_Length-1) , col))
                board(row-1 + (0:Word_Length-1), col-1) = words{ind(w)};
                Used (row   + (0:Word_Length-1), col + (-1:1) ) = true; % left-center-right
                Used (row   + Word_Length      , col  ) = true;         % below
                if row>2 && Used(row-2, col+1)
                    Used(row-1, col+1) = true;                          % top-right
                end
                break
            end
    end
end

end