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

s1-link3-5-7_func_v2-1

by Abhisek Ukil

Status: Passed
Results: 8303945 (cyc: 14, node: 528)
CPU Time: 58.489
Score: 20764.7
Submitted at: 2011-04-14 14:45:03 UTC
Scored at: 2011-04-14 15:01:51 UTC

Current Rank: 1467th (Highest: 50th )

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

board = zeros(n);

N=numel(weights);

count=zeros(N,3);

for i=1:N
    t=numel(words{i});
    count(i,1:3)=[t weights(i) weights(i)/t];
end

[mv,mi]=sort(count(:,3),'descend');
words=words(mi);

wcount=0;
wlist=[];
for i=1:2:n
    L=1;
    while L<n
        wcount=wcount+1;

        if wcount<=N
            s=size(words{wcount},2);

            if L+s-1<=n
                board(i,L:L+s-1)=words{wcount};
                L=L+s+1;
                wlist=[wlist wcount];
            else
                wcount=wcount-1;
                break
            end
        else
            wcount=wcount-1;
            break
        end

    end
end

%------------------- end padding -------------------------

z=[];
for i=1:2:n
    x=find(board(i,:)>0,1,'last');
    z(i)=n-x;
end

nwlist=setdiff(1:N,wlist);
nwords=words(nwlist);

NN=numel(nwlist);
count=zeros(1,NN);

for i=1:NN
    count(i)=numel(nwords{i});
end


L=zeros(1,n);
for i=1:2:n
    if z(i)>2
        [mv,mi]=find(count==z(i)-1,1,'first');
        if ~isempty(mi)
            L(i)=mi;
            count(mi)=-10;
        end
    end
end

for i=1:2:n
    if L(i)>0
        from=n-z(i)+2;
        board(i,from:n)=nwords{L(i)};
    end
end

%-----------------------------------------------------------


for i=3:2:7
    board= func_linking(i,count,nwords,board,n); %length should be odd
end


end

%##################################################################

function board = func_linking(length,count,nwords,board,n)

L=find(count==length);
L_words=nwords(L);

for i=1:n
    temp=board(:,i);
    from=1:(length+1):n;
    to=from+(length-1);

    for j=1:numel(from)
        if from(j)<=n & to(j)<=n
            tword=temp(from(j):to(j));

            for k=1:numel(L)
                mword=L_words{k};

                flag=0;
                
                for ii=1:2:length
                    if mword(ii)~=tword(ii)
                        flag=1;
                        break
                    end
                end
                
                
                if flag==0
                    %mword
                    %tword
                    board(from(j):to(j),i)=mword;
                    L_words{k}=zeros(1,length);

                    break
                end
            end
           
        end
    end
end

end