function [thrustRow, thrustCol] = solver(chart, aIndex, bIndex, maxThrottle)
% Basic solver, does nothing
% Copyright 2010 The MathWorks, Inc.
[rr cc trash] = size(chart);
[ar ac] = ind2sub([rr cc], aIndex);
[br bc] = ind2sub([rr cc], bIndex);
diffr = br-ar;
diffc = bc-ac;
% thrustRow(1) = sign(diffr);
% velr = thrustRow(1);
% thrustCol(1) = sign(diffc);
% velc = thrustRow(1);
pos(1,:) = [ar ac];
velr(1) = 0;
velc(1) = 0;
thrustCol = zeros(1,1001);
thrustRow = zeros(1,1001);
turn = false;
epss = 15;
for k = 2:1001
pos(k,:) = pos(k-1, :) + [velr velc];
if any(pos(k,:) <= 0) | any(pos(k,:)>[rr cc])
% 'ups'
% [[rr cc]
% pos(k,:)]
break;
end
diffr = br-pos(k,1);
diffc = bc-pos(k,2);
if (turn == false) && ((abs(diffr) < epss) && (abs(diffc) < epss))
br = ar;
bc = ac;
turn = true;
diffr = br-pos(k,1);
diffc = bc-pos(k,2);
% ['turn ' num2str(k)]
% [k 1]
elseif (turn == true) && ((abs(diffr) < epss) && (abs(diffc) < epss))
% [k 2]
% ['zuhause ' num2str(k)]
break;
end
velr = velr + chart(pos(k,1), pos(k,2), 1);
velc = velc + chart(pos(k,1), pos(k,2), 2);
if maxThrottle >0
if sign(velr)~=sign(diffr)
thrustRow(k) = -(velr)+sign(diffr);
velr = velr + thrustRow(k);
else
thrustRow(k) = 0;
end
if sign(velc)~=sign(diffc)
thrustCol(k) = -(velc)+sign(diffc);
velc = velc + thrustCol(k);
else
thrustCol(k) = 0;
end
if (pos(k,1) + velr) < 1 |(pos(k,1) + velr) >rr
thrustRow(k) = thrustRow(k) - (pos(k,1) + velr);
velr = thrustRow(k);
end
if (pos(k,2) + velc) < 1 |(pos(k,2) + velc) >cc
thrustCol(k) = thrustCol(k) - (pos(k,2) + velc);
velr = thrustCol(k);
end
if velr == 0
thrustRow(k) = sign(diffr);
velr = thrustRow(k);
end
if velc == 0
thrustCol(k) = sign(diffc);
velc = thrustRow(k);
end
maxThrottle = maxThrottle - thrustCol(k) - thrustRow(k);
end
% if maxThrottle <0
% 'blowout'
% end
end
thrustCol = thrustCol(2:k);
thrustRow = thrustRow(2:k);
end
|