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

vertical 4 (staggering)

by freeb

Status: Passed
Results: 8134903 (cyc: 8, node: 269)
CPU Time: 9.016
Score: 20337.4
Submitted at: 2011-04-14 06:58:49 UTC
Scored at: 2011-04-14 07:02:11 UTC

Current Rank: 1426th (Highest: 17th )
Based on: vertical 2 (diff)

Comments
Please login or create a profile.
Code
function board = solver(words, weights, n, ~)
% solver vertical 4 (staggering)
board = zeros(n);
Free  = true(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 :
    if mod(w,2) % alternance to favorize staggering
        [ROW,COL] = find(Free(2 : 2+n-Word_Length , 2 : 2 : end-1));
        COL = 2 * COL - 1;
    else
        [ROW,COL] = find(Free(2 : 2+n-Word_Length , 3 : 2 : end-1));
        COL = 2 * COL;
    end
    for pos = 1:length(ROW)
        row = ROW(pos)+1; % start at (2,2)
        col = COL(pos)+1;
            if all(Free(row + ( 0:Word_Length-1) , col))
                board(row-1 + (0:Word_Length-1), col-1) = words{ind(w)};
                Free (row   + (0:Word_Length-1), col + (-1:1) ) = false; % left-center-right
                Free (row   + Word_Length      , col  ) = false;         % below
                if row>2 && ~Free(row-2, col+1)
                    Free(row-1, col+1) = false;                          % top-right
                end
                %Done = true;
                break
            end
    end
    %if Done, continue, end
    
    % horizontal placement
end

end