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

Crazy Iwan

by Martin F

Status: Passed
Results: 49160 (cyc: 16, node: 942)
CPU Time: 8.947
Score: 9838.96
Submitted at: 2010-11-12 16:47:03 UTC
Scored at: 2010-11-12 16:48:13 UTC

Current Rank: 2254th (Highest: 155th )

Comments
Please login or create a profile.
Code
function [thrustRow, thrustCol] = solver(chart, aIndex, bIndex, maxThrottle)
[aRow, aCol] = ind2sub(size(chart(:,:,1)),aIndex);
[bRow, bCol] = ind2sub(size(chart(:,:,1)),bIndex);

thrustRow = [];
thrustCol = [];

startX = aRow;
startY = aCol;
speedX = chart(aRow,aCol,1);
speedY = chart(aRow,aCol,2);

zielX = bRow;
zielY = bCol;
endIt = false;

done = 0;
for i = 1:100
    if endIt == false
        [x y startX startY speedX speedY done endIt] = sail3(chart, startX, startY, zielX, zielY, maxThrottle,startX,startY, speedX, speedY, done, aRow, aCol, endIt);
    end
    if abs(x) + abs(y) > 0
        thrustRow = [thrustRow x];
        thrustCol = [thrustCol y];
    end
end
end

function nei = neighbours(B, m, n)
    n1 = [false(m,1) B(:,1:end-1)];
    n2 = [B(:,2:end) false(m,1)];
    n3 = [false(1,n); B(1:end-1,:)];
    n4 = [B(2:end,:); false(1,n)];
    nei = n1 | n2 | n3 | n4 | B;
end

function [scoree] = score(chart, startX, startY, zielX, zielY, maxThrottle, curPosX, curPosY, curSpeedX, curSpeedY)
   [m n muh] = size(chart);
   x = (curPosX - zielX) .* (curPosX - zielX);
   y = (curPosY - zielY) .* (curPosY - zielY);

   s1 = (abs(curSpeedX) + abs(curSpeedY) - maxThrottle) .* (abs(curSpeedX) + abs(curSpeedY) - maxThrottle);

   scoree = x + y + s1;
   ixForbidden = false(size(curPosX));
   ixForbidden = ixForbidden | ((curPosX + curSpeedX) > m);
   ixForbidden = ixForbidden | ((curPosX + curSpeedX) < 1);
   ixForbidden = ixForbidden | ((curPosY + curSpeedY) > n);
   ixForbidden = ixForbidden | ((curPosY + curSpeedY) < 1);
   scoree(ixForbidden) = inf;
end


function [sailZeile sailSpalte nX nY nSpeedX nSpeedY done endIt] = sail3(chart, startX, startY, zielX, zielY, maxThrottle, curPosX, curPosY, curSpeedX, curSpeedY, done, aRow, aCol, endIt)

if done > 0
    zielX = aRow;
    zielY = aCol;
end
if done > 1
     done = done - 1;
end

   [m n muh] = size(chart);
   Nav = false(m,n);
   TargetField = Nav;
   
   startX = curPosX + curSpeedX;
   startY = curPosY + curSpeedY;

   sailBackX = 0;
   sailBackY = 0;
   
   if startX > m 
       sailBackX = m - startX;
   end
   if startX < 1 
       sailBackX = -startX + 1;
   end
   if startY > n
       sailBackY = n - startY;
   end
   if startY < 1
       sailBackY = -startY + 1;
   end
   
   curSpeedX = curSpeedX + sailBackX;
   curSpeedY = curSpeedY + sailBackY;
   
   startX = startX + sailBackX;
   startY = startY + sailBackY;
   
   thisFieldScore = score(chart, startX, startY, zielX, zielY, maxThrottle, curPosX, curPosY, curSpeedX, curSpeedY);
  
   Nav(startX,startY) = true;
   
   for k = 1:maxThrottle-abs(sailBackX)-abs(sailBackY)
       Nav = neighbours(Nav,m,n);
   end
   miX = min(startX, zielX);
   maX = max(startX, zielX);

   miY = min(startY, zielY);
   maY = max(startY, zielY);
   
  TargetField(miX:maX,miY:maY) = true;

  zeilen = repmat([1:m]',1,n) - startX;
  spalten = repmat([1:n],m,1) - startY;
  
  zeilenWind = chart(:,:,1);
  spaltenWind = chart(:,:,2);

  nextStartX = startX + zeilen(Nav);
  nextStartY = startY + spalten(Nav);
  
  nextSpeedX = curSpeedX + zeilen(Nav) + zeilenWind(Nav);
  nextSpeedY = curSpeedY + spalten(Nav) + spaltenWind(Nav);
  
  sC = score(chart, startX, startY, zielX, zielY, maxThrottle, nextStartX, nextStartY, nextSpeedX, nextSpeedY);
  
  ix = sC == min(sC);

  choice = sC(ix);

  z = zeilen(Nav);
  s = spalten(Nav);
    
  sailZeile = z(ix);
  sailSpalte = s(ix);
  nX = nextStartX(ix);
  nY = nextStartY(ix);
  nSpeedX = nextSpeedX(ix);
  nSpeedY = nextSpeedY(ix);
  
  sailZeile = sailZeile(1)+sailBackX;
  sailSpalte = sailSpalte(1)+sailBackY;
  nX = nX(1);
  nY = nY(1);
  nSpeedX = nSpeedX(1);
  nSpeedY = nSpeedY(1);

   if (thisFieldScore < choice(1) & (done == 0)) | ((done==0) & (choice == thisFieldScore))
      done = 3;
      sailZeile = 0;
      sailSpalte = 0;
      nX = curPosX;
      nY = curPosY;
      nSpeedX = curSpeedX;
      nSpeedY = curSpeedY;
      return;
   end
   if (thisFieldScore < choice(1) & (done == 1)) | ((done==1) & (choice == thisFieldScore)) %
      sailZeile = 0;
      sailSpalte = 0;
      nX = curPosX;
      nY = curPosY;
      nSpeedX = curSpeedX;
      nSpeedY = curSpeedY;
      endIt = true;
      return;
   end
end