Finish 2012-11-07 16:00:00 UTC

TangledTrees2

by Greg S

Status: Passed
Results: 28438 (cyc: 12, node: 609)
CPU Time: 0.512
Score: 28658.3
Submitted at: 2012-11-01 14:37:42 UTC
Scored at: 2012-11-01 20:08:58 UTC

Current Rank: 1619th (Highest: 15th )

Comments
Please login or create a profile.
Code
function xyOut = solver(a, xyIn, wts)

dir = [1 0; 1 1; 0 1; -1 1; -1 0; -1 -1; 0 -1; 1 -1];
xyOut = xyIn;
aa = a;
wts=wts';

ZZ =[xyIn(:,1).*wts xyIn(:,2).*wts];
WC = sum(ZZ,1)/sum(wts);


g = zeros(200,200);  %grid of placed points

np = size(xyOut,1); 
d=zeros(np,1); %list of "done" points

sa = sum(a,1);    %sum of a rows, number of cons for each point
[nc,i] = max(sa);

xyOut = xyIn; %REMOVE
%place
cen = [1 50];
xyOut(i,:) = cen;
g(cen(1),cen(2)) = i;
d(i) = 1;
xp = 2;

p2 = find(a(i,:));
t = sa(p2);
[y,i]=sort(t,'descend');
p2 = p2(i);
c = find(triu(a(p2,p2),1));
if ~isempty(c)
   %rearrange 
end


num = length(p2);
yp = 50-floor(num/2);
for i=p2
   %place
   xyOut(i,:) = [xp yp];
   g(xp,yp) = i;
   d(i) = 1;
   yp=yp+1;
end
xp=xp+1;

dp = find(d);
aa(dp,dp) = 0;
sa = sum(aa,1);

lp = p2;
while ~isempty(lp)
    lpt=zeros(1,100);
    nn = 0;
    for i=lp,
        t = sa(i);
        if t>0,  %are there any more points attached to this one, if not move on to next
            p2 = find(aa(i,:));
            if ~isempty(find(d(p2)))
                %place these points first
            end
            c = find(triu(a(p2,p2),1));
            if ~isempty(c)
                %rearrange
            end

            for k=p2
                if isempty(find(lpt==k))
                    nn=nn+1;
                    lpt(nn) = k;
                end
            end
        end
    end

    yp = 50-round(nn/2);
    lpt=lpt(1:nn);
    mbow=ceil(nn/2)-1;
    if mod(nn,2) ==0
        bow=[0:mbow, mbow:-1:0];
    else
        bow=[0:mbow, (mbow-1):-1:0];
    end
    cnt=0;
    for i=lpt
        cnt=cnt+1;
        %place
        xyOut(i,:) = [xp+bow(cnt) yp];
        g(xp,yp) = i;
        d(i) = 1;
        yp=yp+1;
    end
    xp=xp+1+mbow;
    
    lp = lpt;
    dp = find(d);
    aa(dp,dp) = 0;
    sa = sum(aa,1);
    

end


off = [zeros(np,1) 50*ones(np,1)];
xyOut = xyOut-off;

ZZ2 =[xyOut(:,1).*wts xyOut(:,2).*wts];
WC2 = sum(ZZ2,1)/sum(wts);
OFF = round(WC2-WC);
off = [OFF(1)*ones(np,1) OFF(2)*ones(np,1)];

xyOut = xyOut-off;


%xyOut = xyIn;

end