function best_board = rubbish(words, weights, n, penalty)
best_score = -1;
w = [0 0.01*ones(1,5) 0.03*ones(1,5) 0.1*ones(1,5)];
sz = size(weights);
for i=1:numel(w)
r = ones(sz) + w(i)*rand(sz);
[board,score] = solver1(words, r.*weights, n, penalty);
if score>best_score
best_score = score;
best_board = board;
end
end
return
function [board,score] = solver1(words, weights, n, penalty)
% Sample solver
% Copyright 2011 The MathWorks, Inc.
if penalty==0
incr=1;
else
incr=2;
end
board = zeros(n);
nw = numel(words);
imax = zeros(1,n); % maxj(i) is the max depth to which column i is filled
lw = zeros(nw,1);
word_free = true(nw,1);
for j =1:nw;
lw(j) = length(words{j});
end
[weights,isort] = sort(weights./(lw'+1),'descend');
words = words(isort);
lw = lw(isort);
j =1; % column in which to place next word
k = 1; % word to work on
n_placed= 0;
score = 0;
while n_placed<=nw && j<=n
if imax(j) == 0
i_start = 1;
else
i_start = imax(j)+2 ;
end
% most valuable word that will fit:
k = find(lw <= n-i_start+1 & word_free, 1, 'first' );
i_end = i_start + lw(k)-1;
if k
board(i_start:i_end,j)=words{k};
imax(j) = i_end;
word_free(k) = false;
n_placed = n_placed + 1;
score = score + weights(k);
else
j=j+incr;
end
end
return
function board = gridx(words,weights,n,penalty,lw,nw)
wordsmat = cell2mat(words');
k =1;
while k<=nw
wordk = words{k}';
j=1;
while j<=n
% words
end
end
return
|