Finish 2005-05-18 09:00:00 UTC

testc19

by JohanH

Status: Passed
Results: 21243.3884
CPU Time: 79.8566
Score: 2124.71
Submitted at: 2005-05-18 16:59:31 UTC
Scored at: 2005-05-19 04:34:39 UTC

Current Rank: 890th
Based on: soo sneaky (diff)

Comments
Please login or create a profile.
Code
function [dy,dx,mark,carry]=solver(main,food,ants,scent)
%18372.39
rand(1,5);
i35=3:5;
i24=2:4;
i02=0:2;
% find reachable regions
BAD=main<0;
if BAD(7)||BAD(9)||BAD(17)||BAD(19)
    if BAD(7)
        BAD(1)=1;
        BAD(2)=BAD(2)||BAD(8);
        BAD(3)=BAD(3)||(BAD(8)&&BAD(9));
        BAD(6)=BAD(6)||BAD(12);
        BAD(11)=BAD(11)||(BAD(12)&&BAD(17));
    end
    if BAD(9)
        BAD(4)=BAD(4)||BAD(8);
        BAD(5)=1;
        BAD(10)=BAD(10)||BAD(14);
    end
    if BAD(17)
        BAD(16)=BAD(16)||BAD(12);
        BAD(21)=1;
        BAD(22)=BAD(22)||BAD(18);
    end
    if BAD(19)
        BAD(15)=BAD(15)||(BAD(9)&&BAD(14));
        BAD(20)=BAD(20)||(BAD(14));
        BAD(23)=BAD(23)||(BAD(17)&&BAD(18));
        BAD(24)=BAD(24)||(BAD(18));
        BAD(25)=1;
    end
    % modify current board based on reachable regions
    main(BAD)=-1;
    food(BAD)=0;
    scent(BAD)=0;
    ants(BAD)=0;
end
BAD(13)=1;
% setup game stats
scents=scent(scent>0);
maxscent=max(scents(:));
% mark this field
if (max(main(:))>0)
    mark=400-scent(13);
    scent(13)=400;
    init=0;
    hills=find(main>0);
    scent(hills)=1e7;
    food(hills)=0;
    if isempty(scents)
       maxscent=mark;
    end
elseif (maxscent==min(scents(:))) % init phase
    mark=maxscent-scent(13);
    scent(13)=maxscent;
    init=1;
elseif isempty(scents) % nothing is marked
    mark=98;
    scent(13)=scent(13)+98;
    maxscent=mark;
    init=1;
else
    mark=maxscent-1-scent(13);
    scent(13)=maxscent-1;
    init=0;
end
% look around for food
if max(food(:))<1 ||init % nothing found or no way home, search
    target=((main==0)).*(maxscent-scent);
    target(BAD)=-1;
    [y,x]=find(target==max(target(:)));
    if (numel(y)>1)
        r=ceil(rand*numel(y));
        y=y(r);
        x=x(r);
    end
    carry=food(13)>0;
    if (y>1&&y<5&&x>1&&x<5) % direct way
        dy=y-3;
        dx=x-3;
        return;
    end;
    if ((y==1||y==5)&&(x==1||x==5)) % corner
        dy=y-3;
        dx=x-3;
        return;
    end;
    m(7,7)=0;
    m(y+i02,x+i02)=1;
    m=m(i35,i35);
    t=target(i24,i24).*m+m;
    [dy,dx]=find(t==max(t(:)));
    r=ceil(rand*numel(dy));
    dy=dy(r)-2;
    dx=dx(r)-2;
    return;
end;
if (food(13)>1)%&&rand<.95 % go home
    target=scent;
    target(BAD)=-1;
    [y,x]=find(target(i24,i24)==max(target(:)));
    if isempty(y)
       [y,x]=find(target==max(target(:)));
    else
       x = x + 1;
       y = y + 1;
    end
    if (numel(y)>1)
        r=ceil(rand*numel(y));
        y=y(r);
        x=x(r);
    end
    carry=1;
    if (y>1&&y<5&&x>1&&x<5) % direct way
        dy=y-3;
        dx=x-3;
        return;
    end;
    if ((y==1||y==5)&&(x==1||x==5)) % corner
        dy=y-3;
        dx=x-3;
        return;
    end;
    m(7,7)=0;
    m(y+i02,x+i02)=1;
    m=m(i35,i35);
    t=target(i24,i24).*m+m;
    [dy,dx]=find(t==max(t(:)));
    r=ceil(rand*numel(dy));
    dy=dy(r)-2;
    dx=dx(r)-2;
    return;
end;

if (food(13) >= 1) % sit on one food, build track
    target=scent;
    target(BAD)=-1;
    [yhome,xhome]=find(target==max(target(:)));
        search = ones(5);
        %search(i24,i24)=1e8;
        search(13) = 0;
        for i = 1:numel(yhome)
            if xhome(i) <= 2
                if yhome(i) <= 2
                    search([1:9 11 12 16 17 21]) = 0;
                elseif yhome(i) == 3
                    search([1:15]) = 0;
                elseif yhome(i) >= 4
                    search([1:5 7:10 14 15 19 20 25]) = 0;
                end
            elseif xhome(i) == 3
                if yhome(i) <= 2
                    search([1:3 6:8 11 12 16:18 21:23]) = 0;
                elseif yhome(i) >= 4
                    search([3:5 8:10 14 15 18:20 23:25]) = 0;
                end
            elseif xhome(i) >= 4
                if yhome(i) <= 2
                    search([1 6 7 11 12 16:19 21:25]) = 0;
                elseif yhome(i) == 3
                    search([11:25]) = 0;
                elseif yhome(i) >= 4
                    search([1 9 10 14 15 17:20 21:25]) = 0;
                end
            end
        end
    if ~any(search(:))
        search=ones(5);
        search(i24,i24)=1e8;
    end
    carry = 1;
    %lowfood=food.*(scent<scent(13)).*search;
    lowfood=food.*search;
    if sum(lowfood(:))>0
        [yfood,xfood]=find(lowfood==max(lowfood(:)));
        dhere=max(abs(yhome-3),abs(xhome-3));
        runs=numel(find(lowfood(:)>0));
        target2=lowfood;%.*search;
        target2(BAD)=-1;
        while runs % fetch food behind
            [y,x]=find(target2==max(target2(:)));
            if (numel(y)>1)
                r=ceil(rand*numel(y));
                y=y(r);
                x=x(r);
            end
            if (y>1&&y<5&&x>1&&x<5) % direct way
                dy=y-3;
                dx=x-3;
            elseif ((y==1||y==5)&&(x==1||x==5)) % corner
                dy=y-3;
                dx=x-3;
            else
                m(7,7)=0;
                m(y+i02,x+i02)=1;
                m=m(i35,i35);
                t=target2(i24,i24).*m+m;
                [dy,dx]=find(t==max(t(:)));
                r=ceil(rand*numel(dy));
                dy=dy(r)-2;
                dx=dx(r)-2;
            end
            dthere=max(abs(yhome-y),abs(xhome-x));
            if all(dthere>dhere)
                carry=0;
                return;
            end
            target2(y,x)=0;
            runs=runs-1;
        end
    end
    
    if (carry) % go home
        if (numel(yhome)>1)
            r=ceil(rand*numel(yhome));
            yhome=yhome(r);
            xhome=xhome(r);
        end
        if (yhome>1&&yhome<5&&xhome>1&&xhome<5) % direct way
            dy=yhome-3;
            dx=xhome-3;
            return;
        end;
        if ((yhome==1||yhome==5)&&(xhome==1||xhome==5)) % corner
            dy=yhome-3;
            dx=xhome-3;
            return;
        end;
        m(7,7)=0;
        m(yhome+i02,xhome+i02)=1;
        m=m(i35,i35);
        t=target(i24,i24).*m+m;
        [dy,dx]=find(t==max(t(:)));
        r=ceil(rand*numel(dy));
        dy=dy(r)-2;
        dx=dx(r)-2;
    end
    return;
end;
target=max(food-ants,0);
target(BAD)=-1;
[y,x]=find(target==max(target(:)));
if (numel(y)>1)
    r=ceil(rand*numel(y));
    y=y(r);
    x=x(r);
end
carry=0;
if (y>1&&y<5&&x>1&&x<5) % direct way
    dy=y-3;
    dx=x-3;
    return;
end;
if ((y==1||y==5)&&(x==1||x==5)) % corner
    dy=y-3;
    dx=x-3;
    return;
end;
m(7,7)=0;
m(y+i02,x+i02)=1;
m=m(i35,i35);
t=target(i24,i24).*m+m;
[dy,dx]=find(t==max(t(:)));
r=ceil(rand*numel(dy));
dy=dy(r)-2;
dx=dx(r)-2;