function [thrustR, thrustC] = solver2(chart, aIndex, bIndex, maxThrottle)
windR = chart(:,:,1);
windC = chart(:,:,2);
[nR,nC] = size(windR);
[AR,AC] = ind2sub([nR,nC],aIndex);
[BR,BC] = ind2sub([nR,nC],bIndex);
posR = AR;
posC = AC;
velR = 0;
velC = 0;
[X,Y] = meshgrid(-maxThrottle:maxThrottle);
pick = abs(X(:))+abs(Y(:)) < maxThrottle;
tryThrustR = X(pick);
tryThrustC = Y(pick);
goingHome = false;
thrustR = [];
thrustC = [];
for i=1:1000
tryVelR = velR + tryThrustR + windR(posR,posC);
tryVelC = velC + tryThrustC + windC(posR,posC);
tryPosR = posR + tryVelR;
tryPosC = posC + tryVelC;
velFactor = 2;
inside = find(tryPosR<size(windR,1) ...
& tryPosR>1 ...
& tryPosC<size(windR,2) ...
& tryPosC>1 ...
& (tryPosR + velFactor*tryVelR) < size(windR,1) ...
& (tryPosR + velFactor*tryVelR) > 1 ...
& (tryPosC + velFactor*tryVelC) < size(windR,2) ...
& (tryPosC + velFactor*tryVelC) > 1);
if goingHome
distance = (posR-AR).^2 + (posC-AC).^2;
tryDistance = (tryPosR(inside)-AR).^2 + (tryPosC(inside)-AC).^2;
else
distance = (posR-BR).^2 + (posC-BC).^2;
tryDistance = (tryPosR(inside)-BR).^2 + (tryPosC(inside)-BC).^2;
end
improveDistance = inside(tryDistance < distance);
if isempty(improveDistance)
if ~goingHome
goingHome = true;
continue
else
return
end
end
[~,I] = min(abs(tryThrustR(improveDistance))+abs(tryThrustC(improveDistance)));
I = improveDistance(I);
posR = tryPosR(I);
posC = tryPosC(I);
velR = tryVelR(I);
velC = tryVelC(I);
thrustR(end+1) = tryThrustR(I);
thrustC(end+1) = tryThrustC(I);
end
end
|