Finish 2010-11-17 12:00:00 UTC

Sailer

by Yi Cao

Status: Passed
Results: 315933 (cyc: 11, node: 449)
CPU Time: 0.817
Score: 63188.1
Submitted at: 2010-11-10 23:13:52 UTC
Scored at: 2010-11-10 23:17:39 UTC

Current Rank: 2482nd (Highest: 7th )

Comments
Yi Cao
10 Nov 2010
Learning Sailer
Please login or create a profile.
Code
function [thrustRow, thrustCol] = sailer(chart, aIndex, bIndex, maxThrottle)
% First solver. Build up necessary tools

rWind = chart(:,:,1);
cWind = chart(:,:,2);
[nRow,nCol] = size(rWind);
[rHome,cHome] = ind2sub([nRow nCol], aIndex);
[rIsland,cIsland] = ind2sub([nRow,nCol], bIndex);

% Initial condition
Ri = rHome;
Ci = cHome;
vRi = 0;
vCi = 0;
closeR = Ri;
closeC = Ci;
costIsland = distance(closeR,closeC,rIsland,cIsland);
costTotal = costIsland;
m=100;
thrustRow = zeros(1,m);
thrustCol = zeros(1,m);
t=0;
bestMr = 0;
bestMc = 0;
updatef = true;
mT = maxThrottle;
while updatef
    updatef = false;
    rW = rWind(Ri,Ci);
    cW = cWind(Ri,Ci);
    for mR = -mT:mT
        absMr = abs(mR);
        for mC = absMr-mT:mT-absMr            
            [vRf, vCf, Rf, Cf] = transfer(Ri,Ci,vRi,vCi,rW,cW,mR,mC);
            if Rf<1 || Cf<1 || Rf>nRow || Cf>nCol
                continue
            end
            newIsland = distance(Rf,Cf,rIsland,cIsland);
            newCostIsland = min(newIsland,costIsland);
            newMotor = absMr + abs(mC);
            newTotal = newCostIsland + distance(Rf,Cf,rHome,cHome) + newMotor;
            if newTotal < costTotal
                updatef = true;
                costTotal = newTotal;
                bestMr = mR;
                bestMc = mC;
                bestRi = Rf;
                bestCi = Cf;
                bestvRi = vRf;
                bestvCi = vCf;
                bestIsland = newCostIsland;
            end
        end
    end
    if updatef
        t = t + 1;
        if t>m
            m = m*2;
            thrustRow(m)=0;
            thrustCol(m)=0;
        end
        thrustRow(t) = bestMr;
        thrustCol(t) = bestMc;
        Ri = bestRi;
        Ci = bestCi;
        vRi = bestvRi;
        vCi = bestvCi;
        costIsland = bestIsland;
    end
end
thrustRow(t+1:end)=[];
thrustCol(t+1:end)=[];
end

function [vRf, vCf, Rf, Cf] = transfer(Ri,Ci,vRi,vCi,rW,cW,rM,cM)
vRf = vRi + rW + rM;
vCf = vCi + cW + cM;
Rf = Ri + vRf;
Cf = Ci + vCf;
end

function f = distance(Ri,Ci,Rf,Cf)
f = (Ri-Rf)^2 + (Ci-Cf)^2;
end