ID:45982
Title:Lobotomy XI
Author:Gerbert Myburgh
Date:2008-05-01 17:32:20
Score:17795.9571
Result:174527.00 (cyc: 34, node: 8646)
CPU Time:75.6809
Status:Passed
Comments:
Based on:none
Code:
function W = solver(B)
Orig = B;
answer = [];
answer = zeros(0,4);


dimentions = size(B);
rows = dimentions(1);
cols = dimentions(2);

wiresHor = zeros(rows,cols-1);
wiresVer = zeros(rows-1,cols);

uni = unique(B);
uni = uni(find(uni ~= 0));



uniCount = length(uni);
[emptyR emptyC] = find(B==0);

value = [];
amount = [];
weight = [];

for i=1:uniCount
    next = uni(i);
    value = [value; next];
    [placedr placedc] = find(B==next);
    timesFound = length(placedr);
    amount = [amount; timesFound];
    if(timesFound == 1)
        weight = [weight; 0];
    else
        weight = [weight; next*timesFound];
    end;
end;


% Start value vector. (All the unique values in the matrix)
%value
% Number of occurances for each unique value in the matrix
%amount
% The total weight of each value. That is the number of occurances
% multiplied by the value itself. (Note, if the value only occur once it
% has a zero value)
%weight
origWeight = weight;


grid = zeros(rows,cols);
connected = zeros(rows,cols);


while max(weight) > 0

    nextIdx = find(weight == max(weight));
    if length(nextIdx) > 1
        nextIdx = nextIdx(1);
    end;
    nextValue = value(nextIdx);
    weight(nextIdx) = 0;


    checkedOnceR = [];
    checkedOnceC = [];

    [placedr placedc] = find(B==nextValue);

    linkedR = [];
    linkedC = [];
    
    placedr = flipdim(placedr,1);
    placedc = flipdim(placedc,1);
    
    
    currR = placedr(1);
    currC = placedc(1);

    unConnectedR = placedr;
    unConnectedC = placedc;
    
    checkedOnceR = [];
    checkedOnceC = [];    

    % Remove the first value from the chain. 
    placedr = placedr(2:length(placedr));
    placedc = placedc(2:length(placedc));

    %Find first move (Connect two nodes of the same type)
    attempt = 1;
    checkR = placedr(1);
    checkC = placedc(1);
    [unConnectedR,unConnectedC,answer,linkedR,linkedC,validFound] = doNextMove(B,unConnectedR,unConnectedC,answer,placedr,placedc,1,nextValue*2);
    if validFound == 0
        checkedOnceR = [checkedOnceR; checkR];
        checkedOnceC = [checkedOnceC; checkC];
    end;
    while validFound == 0 && length(unConnectedR) > 0
        checkR = placedr(1);
        checkC = placedc(1);
        [unConnectedR,unConnectedC,answer,linkedR,linkedC,validFound] = doNextMove(B,unConnectedR,unConnectedC,answer,placedr,placedc,1,nextValue*2);
        if validFound == 0
            checkedOnceR = [checkedOnceR; checkR];
            checkedOnceC = [checkedOnceC; checkC];
        end;
    end;

%    while validFound == 1 && length(unConnectedR) > 0
    while length(unConnectedR) > 0
        checkR = unConnectedR(1);
        checkC = unConnectedC(1);
        [unConnectedR,unConnectedC,answer,linkedR,linkedC,validFound] = doNextMove(B,unConnectedR,unConnectedC,answer,linkedR,linkedC,0,nextValue);
        if validFound == 0
            checkedOnceR = [checkedOnceR; checkR];
            checkedOnceC = [checkedOnceC; checkC];
        end;
    end;
    
    while length(checkedOnceR) > 0
        [checkedOnceR,checkedOnceC,answer,linkedR,linkedC,validFound] = doNextMove(B,checkedOnceR,checkedOnceC,answer,linkedR,linkedC,0,nextValue);
    end;    

    %Update the grid for next number
    for i=1:length(linkedR)
        if B(linkedR(i),linkedC(i)) == 0
            B(linkedR(i),linkedC(i)) = -1;
        end;
    end;
    
end;



score1 = grade(Orig,answer);







% if score1 > 2000



    B = Orig;
    answer2 = [];
    answer2 = zeros(0,4);


    dimentions = size(B);
    rows = dimentions(1);
    cols = dimentions(2);

    wiresHor = zeros(rows,cols-1);
    wiresVer = zeros(rows-1,cols);

    uni = unique(B);
    uni = uni(find(uni ~= 0));



    uniCount = length(uni);
    [emptyR emptyC] = find(B==0);

    value = [];
    amount = [];
    weight = [];

    for i=1:uniCount
        next = uni(i);
        value = [value; next];
        [placedr placedc] = find(B==next);
        timesFound = length(placedr);
        amount = [amount; timesFound];
        if(timesFound == 1)
            weight = [weight; 0];
        else
            weight = [weight; next*timesFound];
        end;
    end;


    % Start value vector. (All the unique values in the matrix)
    %value
    % Number of occurances for each unique value in the matrix
    %amount
    % The total weight of each value. That is the number of occurances
    % multiplied by the value itself. (Note, if the value only occur once it
    % has a zero value)
    %weight
    origWeight = weight;


    grid = zeros(rows,cols);
    connected = zeros(rows,cols);


    while max(weight) > 0

        nextIdx = find(weight == max(weight));
        if length(nextIdx) > 1
            nextIdx = nextIdx(1);
        end;
        nextValue = value(nextIdx);
        weight(nextIdx) = 0;


        checkedOnceR = [];
        checkedOnceC = [];

        [placedr placedc] = find(B==nextValue);

        linkedR = [];
        linkedC = [];

%         placedr = flipdim(placedr,1);
%         placedc = flipdim(placedc,1);

        currR = placedr(1);
        currC = placedc(1);

        unConnectedR = placedr;
        unConnectedC = placedc;

        checkedOnceR = [];
        checkedOnceC = [];    

        % Remove the first value from the chain. 
        placedr = placedr(2:length(placedr));
        placedc = placedc(2:length(placedc));

        %Find first move (Connect two nodes of the same type)
        attempt = 1;
        checkR = placedr(1);
        checkC = placedc(1);
        [unConnectedR,unConnectedC,answer2,linkedR,linkedC,validFound] = doNextMove(B,unConnectedR,unConnectedC,answer2,placedr,placedc,1,nextValue*2);
        if validFound == 0
            checkedOnceR = [checkedOnceR; checkR];
            checkedOnceC = [checkedOnceC; checkC];
        end;
        while validFound == 0 && length(unConnectedR) > 0
            checkR = placedr(1);
            checkC = placedc(1);
            [unConnectedR,unConnectedC,answer2,linkedR,linkedC,validFound] = doNextMove(B,unConnectedR,unConnectedC,answer2,placedr,placedc,1,nextValue*2);
            if validFound == 0
                checkedOnceR = [checkedOnceR; checkR];
                checkedOnceC = [checkedOnceC; checkC];
            end;
        end;

    %    while validFound == 1 && length(unConnectedR) > 0
        while length(unConnectedR) > 0
            checkR = unConnectedR(1);
            checkC = unConnectedC(1);
            [unConnectedR,unConnectedC,answer2,linkedR,linkedC,validFound] = doNextMove(B,unConnectedR,unConnectedC,answer2,linkedR,linkedC,0,nextValue);
            if validFound == 0
                checkedOnceR = [checkedOnceR; checkR];
                checkedOnceC = [checkedOnceC; checkC];
            end;
        end;

        while length(checkedOnceR) > 0
            [checkedOnceR,checkedOnceC,answer2,linkedR,linkedC,validFound] = doNextMove(B,checkedOnceR,checkedOnceC,answer2,linkedR,linkedC,0,nextValue);
        end;    

        %Update the grid for next number
        for i=1:length(linkedR)
            if B(linkedR(i),linkedC(i)) == 0
                B(linkedR(i),linkedC(i)) = -1;
            end;
        end;

    end;
    
    score2 = grade(Orig,answer2);
    
% else
%     score2 = score1*10;
% end;

% score1
% score2
if (score1 <= score2)
    W = answer;
else
    W = answer2;
end;
% W = answer2;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [unConnectedR,unConnectedC,myanswer,linkedR,linkedC,validFound] = doNextMove(B,unConnectedR,unConnectedC,answer,linkedR,linkedC,first,nextValue)
myanswer = [];
validFound = 0;

currR = unConnectedR(1);
currC = unConnectedC(1);

% Remove the first value from the chain. 
unConnectedR = unConnectedR(2:length(unConnectedR));
unConnectedC = unConnectedC(2:length(unConnectedC));

[lengths] = findClosest(B,currR,currC,linkedR,linkedC);
closest = min(lengths);
closestIndex = find(lengths == closest,1);
gotoR = linkedR(closestIndex);
gotoC = linkedC(closestIndex);

[possibleMoves,useLen1,useLen2,useLen3,useLen4,useGotoR,useGotoC] = calcMove(B,currR,currC,nextValue,gotoR,gotoC);

%FULL CHECK
if min(possibleMoves)== 100000
      [possibleMoves,useLen1,useLen2,useLen3,useLen4,useGotoR,useGotoC] = calcMove(B,currR,currC,nextValue,linkedR,linkedC);
end; 


if min(possibleMoves) ~= 100000
    
    if first==1
        linkedR = [];
        linkedC = [];
        linkedR = [linkedR; currR];
        linkedC = [linkedC; currC];
    end;

    validFound = 1;
%    possibleMoves
    doMove = find(possibleMoves==min(possibleMoves),1);
    
%     if doMove == 20
% %     if doMove == 7 || doMove == 8
%          doMove
%    
%     end;

    newmoves  = [];
    newmoves2 = [];
    newmoves3 = [];    
    switch doMove
        case 1  
            [newmoves,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen1(1),currR,currC);
        case 2  
            [newmoves,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen1(2),currR,currC);
        case 3  
            [newmoves,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen2(3),currR,currC);
        case 4  
            [newmoves,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen2(4),currR,currC);
        case 5
            [newmoves,linkedR,linkedC]  = genMoveU(linkedR,linkedC,B,useLen1(5),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen3(5),useGotoR(5),useGotoC(5));
            [newmoves3,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen2(5),currR-useLen1(5),currC);
        case 6
            [newmoves,linkedR,linkedC]  = genMoveU(linkedR,linkedC,B,useLen1(6),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen3(6),useGotoR(6),useGotoC(6));
            [newmoves3,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen2(6),currR-useLen1(6),currC);
        case 7
            [newmoves,linkedR,linkedC]  = genMoveR(linkedR,linkedC,B,useLen2(7),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen4(7),useGotoR(7),useGotoC(7));
            [newmoves3,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen1(7),currR,currC+useLen2(7));
        case 8
            [newmoves,linkedR,linkedC]  = genMoveR(linkedR,linkedC,B,useLen2(8),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen4(8),useGotoR(8),useGotoC(8));
            [newmoves3,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen1(8),currR,currC+useLen2(8));
        case 9
            [newmoves,linkedR,linkedC]  = genMoveD(linkedR,linkedC,B,useLen1(9),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen3(9),useGotoR(9),useGotoC(9));
            [newmoves3,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen2(9),currR+useLen1(9),currC);
        case 10
            [newmoves,linkedR,linkedC]  = genMoveD(linkedR,linkedC,B,useLen1(10),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen3(10),useGotoR(10),useGotoC(10));
            [newmoves3,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen2(10),currR+useLen1(10),currC);
        case 11
            [newmoves,linkedR,linkedC]  = genMoveL(linkedR,linkedC,B,useLen2(11),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen4(11),useGotoR(11),useGotoC(11));
            [newmoves3,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen1(11),currR,currC-useLen2(11));
        case 12
            [newmoves,linkedR,linkedC]  = genMoveL(linkedR,linkedC,B,useLen2(12),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen4(12),useGotoR(12),useGotoC(12));
            [newmoves3,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen1(12),currR,currC-useLen2(12));
        case 13
            [newmoves,linkedR,linkedC]  = genMoveR(linkedR,linkedC,B,useLen2(13),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen4(13),useGotoR(13),useGotoC(13));
            [newmoves3,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen1(13),currR,currC+useLen2(13));
        case 14
            [newmoves,linkedR,linkedC]  = genMoveL(linkedR,linkedC,B,useLen2(14),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen4(14),useGotoR(14),useGotoC(14));
            [newmoves3,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen1(14),currR,currC-useLen2(14));
        case 15
            [newmoves,linkedR,linkedC]  = genMoveR(linkedR,linkedC,B,useLen2(15),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen4(15),useGotoR(15),useGotoC(15));
            [newmoves3,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen1(15),currR,currC+useLen2(15));
        case 16
            [newmoves,linkedR,linkedC]  = genMoveL(linkedR,linkedC,B,useLen2(16),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen4(16),useGotoR(16),useGotoC(16));
            [newmoves3,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen1(16),currR,currC-useLen2(16));
        case 17
            [newmoves,linkedR,linkedC]  = genMoveD(linkedR,linkedC,B,useLen1(17),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen2(17),currR+useLen1(17),currC);
            [newmoves3,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen3(17),useGotoR(17),useGotoC(17));
        case 18
            [newmoves,linkedR,linkedC]  = genMoveU(linkedR,linkedC,B,useLen1(18),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen2(18),currR-useLen1(18),currC);
            [newmoves3,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen3(18),useGotoR(18),useGotoC(18));
        case 19
            [newmoves,linkedR,linkedC]  = genMoveD(linkedR,linkedC,B,useLen1(19),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen2(19),currR+useLen1(19),currC);
            [newmoves3,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen3(19),useGotoR(19),useGotoC(19));
        case 20
            [newmoves,linkedR,linkedC]  = genMoveU(linkedR,linkedC,B,useLen1(20),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen2(20),currR-useLen1(20),currC);
            [newmoves3,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen3(20),useGotoR(20),useGotoC(20));
        case 21
            [newmoves,linkedR,linkedC]  = genMoveU(linkedR,linkedC,B,useLen1(21),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen2(21),useGotoR(21),currC);
        case 22
            [newmoves,linkedR,linkedC]  = genMoveL(linkedR,linkedC,B,useLen2(22),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen1(22),currR,useGotoC(22));
        case 23
            [newmoves,linkedR,linkedC]  = genMoveD(linkedR,linkedC,B,useLen1(23),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,useLen2(23),useGotoR(23),currC);
        case 24
            [newmoves,linkedR,linkedC]  = genMoveL(linkedR,linkedC,B,useLen2(24),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen1(24),currR,useGotoC(24));
        case 25
            [newmoves,linkedR,linkedC]  = genMoveR(linkedR,linkedC,B,useLen2(25),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveU(linkedR,linkedC,B,useLen1(25),currR,useGotoC(25));
        case 26
            [newmoves,linkedR,linkedC]  = genMoveD(linkedR,linkedC,B,useLen1(26),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen2(26),useGotoR(26),currC);
        case 27
            [newmoves,linkedR,linkedC]  = genMoveR(linkedR,linkedC,B,useLen2(27),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveD(linkedR,linkedC,B,useLen1(27),currR,useGotoC(27));
        case 28
            [newmoves,linkedR,linkedC]  = genMoveU(linkedR,linkedC,B,useLen1(28),currR,currC);
            [newmoves2,linkedR,linkedC] = genMoveL(linkedR,linkedC,B,useLen2(28),useGotoR(28),currC);
        otherwise        % No valid move found
    end;

    if first==1
        idx = find(unConnectedR==useGotoR(doMove));
        p1 = unConnectedR(1:idx-1);
        p2 = unConnectedR(idx+1:length(unConnectedR));
        unConnectedR = [p1;p2];

        p1 = unConnectedC(1:idx-1);
        p2 = unConnectedC(idx+1:length(unConnectedC));
        unConnectedC = [p1;p2];    
    end;
    
    myanswer = [answer; newmoves; newmoves2; newmoves3];

else
    myanswer = answer;
    
    noMoveFound = 1;
end;


function [possibleMoves,useLen1,useLen2,useLen3,useLen4,useGotoR,useGotoC] = calcMove(B,currR,currC,nextValue,linkedR,linkedC)
possibleMoves = ones(28,1)*100000;

useLen1 = zeros(28,1);
useLen2 = zeros(28,1);
useLen3 = zeros(28,1);
useLen4 = zeros(28,1);

useGotoR = zeros(28,1);
useGotoC = zeros(28,1);

len1 = zeros(28,1); % First horizontal distance
len2 = zeros(28,1); % First vertical distance
len3 = zeros(28,1); % Second horizontal distance
len4 = zeros(28,1); % Second vertical distance
jumps = zeros(28,1);

for aaa=1:length(linkedR)
    gotoR = linkedR(aaa);
    gotoC = linkedC(aaa);
    [len1(1),jumps(1)] = checkRight(B,currR,currC,gotoR,gotoC,0,nextValue);
    [len1(2),jumps(2)] = checkLeft(B,currR,currC,gotoR,gotoC,0,nextValue);
    [len2(3),jumps(3)] = checkUp(B,currR,currC,gotoR,gotoC,0,nextValue);
    [len2(4),jumps(4)] = checkDown(B,currR,currC,gotoR,gotoC,0,nextValue);
    
    
    if len1(1) <= 0 && len1(2) <= 0 && len2(3) <= 0 && len2(4) <= 0

        [len1(21),len2(21),jumps(21)] = checkUpRight(B,currR,currC,gotoR,gotoC,nextValue);
        [len1(22),len2(22),jumps(22)] = checkLeftDown(B,currR,currC,gotoR,gotoC,nextValue);
        [len1(23),len2(23),jumps(23)] = checkDownRight(B,currR,currC,gotoR,gotoC,nextValue);
        [len1(24),len2(24),jumps(24)] = checkLeftUp(B,currR,currC,gotoR,gotoC,nextValue);
        [len1(25),len2(25),jumps(25)] = checkRightUp(B,currR,currC,gotoR,gotoC,nextValue);
        [len1(26),len2(26),jumps(26)] = checkDownLeft(B,currR,currC,gotoR,gotoC,nextValue);
        [len1(27),len2(27),jumps(27)] = checkRightDown(B,currR,currC,gotoR,gotoC,nextValue);
        [len1(28),len2(28),jumps(28)] = checkUpLeft(B,currR,currC,gotoR,gotoC,nextValue);
        
        if len1(21) <= 0 && len1(22) <= 0 && len1(23) <= 0 && len1(24) <= 0 && len1(25) <= 0 && len1(26) <= 0 && len1(27) <= 0 && len1(28) <= 0
            [len1(13),len2(13),len3(13),len4(13),jumps(13)] = bounceRightUpRight(B,currR,currC,gotoR,gotoC,nextValue);
            [len1(14),len2(14),len3(14),len4(14),jumps(14)] = bounceLeftDownLeft(B,currR,currC,gotoR,gotoC,nextValue);
            [len1(15),len2(15),len3(15),len4(15),jumps(15)] = bounceRightDownRight(B,currR,currC,gotoR,gotoC,nextValue);
            [len1(16),len2(16),len3(16),len4(16),jumps(16)] = bounceLeftUpLeft(B,currR,currC,gotoR,gotoC,nextValue);

            [len1(17),len2(17),len3(17),len4(17),jumps(17)] = bounceDownRightDown(B,currR,currC,gotoR,gotoC,nextValue);
            [len1(18),len2(18),len3(18),len4(18),jumps(18)] = bounceUpLeftUp(B,currR,currC,gotoR,gotoC,nextValue);
            [len1(19),len2(19),len3(19),len4(19),jumps(19)] = bounceDownLeftDown(B,currR,currC,gotoR,gotoC,nextValue);
            [len1(20),len2(20),len3(20),len4(20),jumps(20)] = bounceUpRightUp(B,currR,currC,gotoR,gotoC,nextValue);
            
            if len1(13) <= 0 && len1(14) <= 0 && len1(15) <= 0 && len1(16) <= 0 && len1(17) <= 0 && len1(18) <= 0 && len1(19) <= 0 && len1(20) <= 0
                [len1(5),len2(5),len3(5),len4(5),jumps(5)] = bounceTop(B,currR,currC,gotoR,gotoC,nextValue);
                [len1(6),len2(6),len3(6),len4(6),jumps(6)] = bounceTop2(B,currR,currC,gotoR,gotoC,nextValue);
                [len1(7),len2(7),len3(7),len4(7),jumps(7)] = bounceRight(B,currR,currC,gotoR,gotoC,nextValue);
                [len1(8),len2(8),len3(8),len4(8),jumps(8)] = bounceRight2(B,currR,currC,gotoR,gotoC,nextValue);
                [len1(9),len2(9),len3(9),len4(9),jumps(9)] = bounceBottom(B,currR,currC,gotoR,gotoC,nextValue);
                [len1(10),len2(10),len3(10),len4(10),jumps(10)] = bounceBottom(B,currR,currC,gotoR,gotoC,nextValue);
                [len1(11),len2(11),len3(11),len4(11),jumps(11)] = bounceLeft(B,currR,currC,gotoR,gotoC,nextValue);
                [len1(12),len2(12),len3(12),len4(12),jumps(12)] = bounceLeft2(B,currR,currC,gotoR,gotoC,nextValue);
            end;
        end;
    end;
        

    for i=1:28    
        if len1(i) >= 0 && len2(i) >= 0 && len3(i) >= 0 && len4(i) >= 0 
            sum = len1(i)+len2(i)+len3(i)+len4(i)+jumps(i)*25;
            if (sum < possibleMoves(i)) && (sum > 0)
                useLen1(i) = len1(i);
                useLen2(i) = len2(i);       
                useLen3(i) = len3(i);
                useLen4(i) = len4(i);
                useGotoR(i) = gotoR;
                useGotoC(i) = gotoC;                
                possibleMoves(i) = sum;
            end;
        end;
        if possibleMoves(i) == 0
            possibleMoves(i) = 100000;
        end;
    end;
end;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [lengths] = findClosest(B,currR,currC,placedr,placedc)
possible = length(placedr);
lengths = [];
for i=1:possible
    len = 0;
    len = abs(currR - placedr(i)) + abs(currC - placedc(i));
    lengths = [lengths; len];
end;


function [newmoves,linkedR,linkedC] = genMoveR(linkedR,linkedC,B,dist,currR,currC)
%dist
newmoves = [];
col = currC;
linkedR = [linkedR; currR];
linkedC = [linkedC; currC];
for i=1:dist
    newmoves = [newmoves; currR col currR col+1];
    linkedR = [linkedR; currR];
    linkedC = [linkedC; col+1];
    if B(currR,col+1)==-1
        newmoves = [newmoves; currR col+1 currR col+1];
    end;
    lastR = currR;
    lastC = col+1;
    col = col + 1;
end;

function [newmoves,linkedR,linkedC,lastR,lastC] = genMoveL(linkedR,linkedC,B,dist,currR,currC)
newmoves = [];
col = currC;
linkedR = [linkedR; currR];
linkedC = [linkedC; currC];
for i=1:dist
    newmoves = [newmoves; currR col currR col-1];
    linkedR = [linkedR; currR];
    linkedC = [linkedC; col-1];
    if B(currR,col-1)==-1
        newmoves = [newmoves; currR col-1 currR col-1];
    end;    lastR = currR;
    lastC = col-1;
    col = col - 1;
end;

function [newmoves,linkedR,linkedC,lastR,lastC] = genMoveD(linkedR,linkedC,B,dist,currR,currC)
newmoves = [];
row = currR;
linkedR = [linkedR; currR];
linkedC = [linkedC; currC];
for i=1:dist
    newmoves = [newmoves; row currC row+1 currC];
    linkedR = [linkedR; row+1];
    linkedC = [linkedC; currC];
    if B(row+1,currC)==-1
        newmoves = [newmoves; row+1 currC row+1 currC];
    end;
    lastR = row+1;
    lastC = currC;
    row = row + 1;
end;

function [newmoves,linkedR,linkedC,lastR,lastC] = genMoveU(linkedR,linkedC,B,dist,currR,currC)
newmoves = [];
row = currR;
linkedR = [linkedR; currR];
linkedC = [linkedC; currC];
for i=1:dist
    newmoves = [newmoves; row currC row-1 currC];
    linkedR = [linkedR; row-1];
    linkedC = [linkedC; currC];
    if B(row-1,currC)==-1
        newmoves = [newmoves; row-1 currC row-1 currC];
    end;
    lastR = row-1;
    lastC = currC;
    row = row - 1;
end;


% MOVE 1
function [len,jump] = checkLeft(B,currR,currC,toR,toC,corner,nextValue)
clear = 1;
len = -1;
jump = 0;
if currR == toR
    if corner == 0
        go = toC+1;
    else
        go = toC;
    end;
    for i=go:currC-1
        if B(currR,i) ~= 0
             if i>1 && i<size(B,2) && B(currR,i) == -1 && (B(currR,i-1)==0 || B(currR,i-1)==nextValue)&& (B(currR,i+1)==0 || B(currR,i+1)==nextValue) && nextValue>25
                 nextValue = nextValue - 25;
                 jump = jump+1;
             else
                 clear = 0;
             end;

        end;
    end;
    if clear == 1
        len = currC-toC;
    end;
end;


% MOVE 2
function [len,jump] = checkRight(B,currR,currC,toR,toC,corner,nextValue)
clear = 1;
len = -1;
jump = 0;
if currR == toR
    if corner == 0
        go = toC-1;
    else
        go = toC;
    end;    
    for i=currC+1:go
        if B(currR,i) ~= 0
            if i>1 && i<size(B,2) && B(currR,i) == -1 && (B(currR,i-1)==0 || B(currR,i-1)==nextValue)&& (B(currR,i+1)==0 || B(currR,i+1)==nextValue) && nextValue>25
                nextValue = nextValue - 25;
                jump = jump+1;                
            else
                clear = 0;
            end;
        end;
    end;
    if clear == 1
        len = toC-currC;
    end;
end;

% MOVE 3
function [len,jump] = checkUp(B,currR,currC,toR,toC,corner,nextValue)
clear = 1;
len = -1;
jump = 0;
if currC == toC
    if corner == 0
        go = toR+1;
    else
        go = toR;
    end;    
    for i=go:currR-1
        if B(i,currC) ~= 0
            if i>1 && i<size(B,1) && B(i,currC) == -1 && (B(i-1,currC)==0 || B(i-1,currC)==nextValue)&& (B(i+1,currC)==0 || B(i+1,currC)==nextValue) && nextValue>25
                nextValue = nextValue - 25;
                jump = jump+1;                
            else
                clear = 0;
            end;
        end;
    end;
    if clear == 1
        len = currR-toR;
    end;
end;
    

% MOVE 4
function [len,jump] = checkDown(B,currR,currC,toR,toC,corner,nextValue)
clear = 1;
len = -1;
jump = 0;
if currC == toC
    if corner == 0
        go = toR-1;
    else
        go = toR;
    end;    
    for i=currR+1:go
        if B(i,currC) ~= 0
            if i>1 && i<size(B,1) && B(i,currC) == -1 && B(i-1,currC)==0 && B(i+1,currC)==0 && nextValue>25
                nextValue = nextValue - 25;                
                jump = jump+1;
            else
                clear = 0;
            end;
        end;
    end;
    if clear == 1
        len = toR-currR;
    end;
end;

% MOVE 5
function [len1,len2,len3,len4,jump] = bounceTop(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
cost = 100000;
if currR > 1
    for i=1:currR-1
        [testLen2,j2] = checkRight(B,i,currC,i,toC,0,nextValue);
        testLen1 = -1;  testLen3 = -1;
        j1 = 0; j3 = 0;
        if testLen2 > 1
            [testLen1,j2] = checkUp(B,currR,currC,i,currC,1,nextValue);
            [testLen3,j3] = checkUp(B,toR,toC,i,toC,1,nextValue);
        end;
        if testLen1 > 0 && testLen3 > 0
            currCost = testLen1 + testLen2 + testLen3 + testLen4 +(j1+j2+j3+j4)*25;
            if currCost < cost
                cost = currCost;
                len1 = testLen1;
                len2 = testLen2;
                len3 = testLen3;
                len4 = 0;
                jump = j1+j2+j3+j4;
            end;
        end;
    end;
end;

% MOVE 6
function [len1,len2,len3,len4,jump] = bounceTop2(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
cost = 100000;
if currR > 1
    for i=1:currR-1
        [testLen2,j2] = checkLeft(B,i,currC,i,toC,0,nextValue);
        testLen1 = -1;  testLen3 = -1;
        j1 = 0; j3 = 0;
        if testLen2 > 1
            [testLen1,j1]  = checkUp(B,currR,currC,i,currC,1,nextValue);
            [testLen3,j3]  = checkUp(B,toR,toC,i,toC,1,nextValue);
        end;
        if testLen1 > 0 && testLen3 > 0
            currCost = testLen1 + testLen2 + testLen3 + testLen4 +(j1+j2+j3+j4)*25;
            if currCost < cost
                cost = currCost;
                len1 = testLen1;
                len2 = testLen2;
                len3 = testLen3;
                len4 = 0;
                jump = j1+j2+j3+j4;
            end;
        end;
    end;
end;

% MOVE 7
function [len1,len2,len3,len4,jump] = bounceRight(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
cost = 100000;
if currC < size(B,2)
    for i=currC+1:size(B,2)
        [testLen1,j1]  = checkUp(B,currR,i,toR,i,0,nextValue);
        testLen2 = -1; testLen4 = -1;
        j2 = 0; j4 = 0;
        if testLen1 > 1
            [testLen2,j2]  = checkRight(B,currR,currC,currR,i,1,nextValue);
            [testLen4,j4]  = checkRight(B,toR,toC,toR,i,1,nextValue);
        end;
        if testLen2 > 0 && testLen4 > 0
            currCost = testLen1 + testLen2 + testLen3 + testLen4 +(j1+j2+j3+j4)*25;
            if currCost < cost
                cost = currCost;
                len1 = testLen1;
                len2 = testLen2;
                len3 = 0;
                len4 = testLen4;
                jump = j1+j2+j3+j4;
            end;
        end;
    end;
end;

% MOVE 8
function [len1,len2,len3,len4,jump] = bounceRight2(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
cost = 100000;
if currC < size(B,2)
    for i=currC+1:size(B,2)
        [testLen1,j1]  = checkDown(B,currR,i,toR,i,0,nextValue);
        testLen2 = -1; testLen4 = -1;
        j2 = 0; j4 = 0; 
        if testLen1 > 1
            [testLen2,j2]  = checkRight(B,currR,currC,currR,i,1,nextValue);
            [testLen4,j4]  = checkRight(B,toR,toC,toR,i,1,nextValue);
        end;
        if testLen2 > 0 && testLen4 > 0
            currCost = testLen1 + testLen2 + testLen3 + testLen4 +(j1+j2+j3+j4)*25;
            if currCost < cost
                cost = currCost;
                len1 = testLen1;
                len2 = testLen2;
                len3 = 0;
                len4 = testLen4;
                jump = j1+j2+j3+j4;
            end;
        end;
    end;
end;

% MOVE 9
function [len1,len2,len3,len4,jump] = bounceBottom(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
cost = 100000;
if currR < size(B,1)
    for i=currR+1:size(B,1)
        [testLen2,j2]  = checkRight(B,i,currC,i,toC,0,nextValue);
        testLen1 = -1;  testLen3 = -1;
        j1 = 0; j3 = 0;
        if testLen2 > 1
            [testLen1,j1]  = checkDown(B,currR,currC,i,currC,1,nextValue);
            [testLen3,j3]  = checkDown(B,toR,toC,i,toC,1,nextValue);
        end;
        if testLen1 > 0 && testLen3 > 0
            currCost = testLen1 + testLen2 + testLen3 + testLen4 +(j1+j2+j3+j4)*25;
            if currCost < cost
                cost = currCost;
                len1 = testLen1;
                len2 = testLen2;
                len3 = testLen3;
                len4 = 0;
                jump = j1+j2+j3+j4;
            end;
        end;
    end;
end;

% MOVE 10
function [len1,len2,len3,len4,jump] = bounceBottom2(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
cost = 100000;
if currR < size(B,1)
    for i=currR+1:size(B,1)
        [testLen2,j2]  = checkLeft(B,i,currC,i,toC,0,nextValue);
        testLen1 = -1;  testLen3 = -1;
        j1 = 0; j3 = 0;
        if testLen2 > 1
            [testLen1,j1]  = checkDown(B,currR,currC,i,currC,1,nextValue);
            [testLen3,j3]  = checkDown(B,toR,toC,i,toC,1,nextValue);
        end;
        if testLen1 > 0 && testLen3 > 0
            currCost = testLen1 + testLen2 + testLen3 + testLen4 +(j1+j2+j3+j4)*25;
            if currCost < cost
                cost = currCost;
                len1 = testLen1;
                len2 = testLen2;
                len3 = testLen3;
                len4 = 0;
                jump = j1+j2+j3+j4;
            end;
        end;
    end;
end;

% MOVE 11
function [len1,len2,len3,len4,jump] = bounceLeft(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
cost = 100000;
if currC > 1
    for i=1:currC-1
        [testLen1,j1]  = checkUp(B,currR,i,toR,i,0,nextValue);
        testLen2 = -1; testLen4 = -1;
        j2 = 0; j4 = 0;
        if testLen1 > 1
            [testLen2,j2]  = checkLeft(B,currR,currC,currR,i,1,nextValue);
            [testLen4,j4]  = checkLeft(B,toR,toC,toR,i,1,nextValue);
        end;
        if testLen2 > 0 && testLen4 > 0
            currCost = testLen1 + testLen2 + testLen3 + testLen4 +(j1+j2+j3+j4)*25;
            if currCost < cost
                cost = currCost;
                len1 = testLen1;
                len2 = testLen2;
                len3 = 0;
                len4 = testLen4;
                jump = j1+j2+j3+j4;
            end;
        end;
    end;
end;

% MOVE 12
function [len1,len2,len3,len4,jump] = bounceLeft2(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
cost = 100000;
if currC > 1
    for i=1:currC-1
        [testLen1,j1]  = checkDown(B,currR,i,toR,i,0,nextValue);
        testLen2 = -1; testLen4 = -1;
        j2 = 0; j4 = 0;
        if testLen1 > 1
            [testLen2,j2]  = checkLeft(B,currR,currC,currR,i,1,nextValue);
            [testLen4,j4]  = checkLeft(B,toR,toC,toR,i,1,nextValue);
        end;
        if testLen2 > 0 && testLen4 > 0
            currCost = testLen1 + testLen2 + testLen3 + testLen4 +(j1+j2+j3+j4)*25;
            if currCost < cost
                cost = currCost;
                len1 = testLen1;
                len2 = testLen2;
                len3 = 0;
                len4 = testLen4;
                jump = j1+j2+j3+j4;
            end;
        end;
    end;
end;

% MOVE 13
function [len1,len2,len3,len4,jump] = bounceRightUpRight(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
if currR < size(B,1)
    for i=currC+1:toC-1
        [testLen1,j1]  = checkUp(B,currR,i,toR,i,0,nextValue);
        testLen2 = -1; testLen4 = -1;
        j2 = 0; j4 = 0;
        if testLen1 >= 1
            [testLen2,j2]  = checkRight(B,currR,currC,currR,i,1,nextValue);
            [testLen4,j4]  = checkLeft(B,toR,toC,toR,i,1,nextValue);
        end;
        if testLen2 > 0 && testLen4 > 0
            len1 = testLen1;
            len2 = testLen2;
            len3 = 0;
            len4 = testLen4;
            jump = j1+j2+j3+j4;
        end;
    end;
end;

% MOVE 14
function [len1,len2,len3,len4,jump] = bounceLeftDownLeft(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
if currR > 1
    for i=toC+1:currC-1
        [testLen1,j1]  = checkDown(B,currR,i,toR,i,0,nextValue);
        testLen2 = -1; testLen4 = -1;
        j2 = 0; j4 = 0;
        if testLen1 >= 1
            [testLen2,j2]  = checkLeft(B,currR,currC,currR,i,1,nextValue);
            [testLen4,j4]  = checkRight(B,toR,toC,toR,i,1,nextValue);
        end;
        if testLen2 > 0 && testLen4 > 0
            len1 = testLen1;
            len2 = testLen2;
            len3 = 0;
            len4 = testLen4;
            jump = j1+j2+j3+j4;
        end;
    end;
end;

% MOVE 15
function [len1,len2,len3,len4,jump] = bounceRightDownRight(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
if currR < size(B,1)
    for i=currC+1:toC-1
        [testLen1,j1]  = checkDown(B,currR,i,toR,i,0,nextValue);
        testLen2 = -1; testLen4 = -1;
        j2 = 0; j4 = 0;
        if testLen1 >= 1
            [testLen2,j2]  = checkRight(B,currR,currC,currR,i,1,nextValue);
            [testLen4,j4]  = checkLeft(B,toR,toC,toR,i,1,nextValue);
        end;
        if testLen2 > 0 && testLen4 > 0
            len1 = testLen1;
            len2 = testLen2;
            len3 = 0;
            len4 = testLen4;
            jump = j1+j2+j3+j4;
        end;
    end;
end;

% MOVE 16
function [len1,len2,len3,len4,jump] = bounceLeftUpLeft(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
if currR > 1
    for i=toC+1:currC-1
        [testLen1,j1]  = checkUp(B,currR,i,toR,i,0,nextValue);
        testLen2 = -1; testLen4 = -1;
        j2 = 0; j4 = 0;
        if testLen1 >= 1
            [testLen2,j2]  = checkLeft(B,currR,currC,currR,i,1,nextValue);
            [testLen4,j4]  = checkRight(B,toR,toC,toR,i,1,nextValue);
        end;
        if testLen2 > 0 && testLen4 > 0
            len1 = testLen1;
            len2 = testLen2;
            len3 = 0;
            len4 = testLen4;
            jump = j1+j2+j3+j4;
        end;
    end;
end;

% MOVE 17
function [len1,len2,len3,len4,jump] = bounceDownRightDown(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
for i=currR+1:toR-1
    [testLen2,j2]  = checkRight(B,i,currC,i,toC,0,nextValue);
    testLen1 = -1; testLen3 = -1;
    j1 = 0; j3 = 0;
    if testLen2 >= 1
        [testLen1,j1]  = checkDown(B,currR,currC,i,currC,1,nextValue);
        [testLen3,j3]  = checkUp(B,toR,toC,i,toC,1,nextValue);
    end;
    if testLen1 > 0 && testLen3 > 0
        len1 = testLen1;
        len2 = testLen2;
        len3 = testLen3;
        len4 = 0;
        jump = j1+j2+j3+j4;
    end;
end;

% MOVE 18
function [len1,len2,len3,len4,jump] = bounceUpLeftUp(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
for i=toR+1:currR-1
    [testLen2,j2]  = checkLeft(B,i,currC,i,toC,0,nextValue);
    testLen1 = -1; testLen3 = -1;
    j1 = 0; j3 = 0;
    if testLen2 >= 1
        [testLen1,j1]  = checkUp(B,currR,currC,i,currC,1,nextValue);
        [testLen3,j3]  = checkDown(B,toR,toC,i,toC,1,nextValue);
    end;
    if testLen1 > 0 && testLen3 > 0
        len1 = testLen1;
        len2 = testLen2;
        len3 = testLen3;
        len4 = 0;
        jump = j1+j2+j3+j4;
    end;
end;

% MOVE 19
function [len1,len2,len3,len4,jump] = bounceDownLeftDown(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
for i=currR+1:toR-1
    [testLen2,j2]  = checkLeft(B,i,currC,i,toC,0,nextValue);
    testLen1 = -1; testLen3 = -1;
    j1 = 0; j3 = 0;
    if testLen2 >= 1
        [testLen1,j1]  = checkDown(B,currR,currC,i,currC,1,nextValue);
        [testLen3,j3]  = checkUp(B,toR,toC,i,toC,1,nextValue);
    end;
    if testLen1 > 0 && testLen3 > 0
        len1 = testLen1;
        len2 = testLen2;
        len3 = testLen3;
        len4 = 0;
        jump = j1+j2+j3+j4;
    end;
end;

% MOVE 20
function [len1,len2,len3,len4,jump] = bounceUpRightUp(B,currR,currC,toR,toC,nextValue)
len1 = -1; len2 = -1; len3 = -1; len4 = -1;
testLen2 = -1; testLen2 = -1; testLen3 = -1; testLen4 = -1;
j1 = 0; j2 = 0; j3 = 0; j4 = 0; jump = 0;
for i=toR+1:currR-1
    [testLen2,j2]  = checkRight(B,i,currC,i,toC,0,nextValue);
    testLen1 = -1; testLen3 = -1;
    j1 = 0; j3 = 0;
    if testLen2 >= 1
        [testLen1,j1]  = checkUp(B,currR,currC,i,currC,1,nextValue);
        [testLen3,j3]  = checkDown(B,toR,toC,i,toC,1,nextValue);
    end;
    if testLen1 > 0 && testLen3 > 0
        len1 = testLen1;
        len2 = testLen2;
        len3 = testLen3;
        len4 = 0;
        jump = j1+j2+j3+j4;
    end;
end;


% MOVE 21
function [len1,len2,jump] = checkUpRight(B,currR,currC,toR,toC,nextValue)
len1 = -1; 
len2 = -1;
j1 = 0; j2 = 0; jump = 0;
if (currR ~= toR) && (currC ~= toC) && (currR > 1)
    [len1,j1]  = checkUp(B,currR,currC,toR,currC,1,nextValue);
    [len2,j2] = checkRight(B,toR,currC,toR,toC,0,nextValue);
    jump = j1+j2;
end;

% MOVE 22
function [len1,len2,jump] = checkLeftDown(B,currR,currC,toR,toC,nextValue)
len1 = -1; 
len2 = -1;
j1 = 0; j2 = 0; jump = 0;
if (currR ~= toR) && (currC ~= toC) && (currR < size(B,1))
    [len1,j1] = checkDown(B,currR,toC,toR,toC,0,nextValue);
    [len2,j2] = checkLeft(B,currR,currC,currR,toC,1,nextValue);
    jump = j1+j2;
end;

% MOVE 23
function [len1,len2,jump] = checkDownRight(B,currR,currC,toR,toC,nextValue)
len1 = -1; 
len2 = -1;
j1 = 0; j2 = 0; jump = 0;
if (currR ~= toR) && (currC ~= toC) && (currR < size(B,1))
    [len1,j1] = checkDown(B,currR,currC,toR,currC,1,nextValue);
    [len2,j2] = checkRight(B,toR,currC,toR,toC,0,nextValue);
    jump = j1+j2;
end;

% MOVE 24
function [len1,len2,jump] = checkLeftUp(B,currR,currC,toR,toC,nextValue)
len1 = -1; 
len2 = -1;
j1 = 0; j2 = 0; jump = 0;
if (currR ~= toR) && (currC ~= toC) && (currR > 1)
    [len1,j1] = checkUp(B,currR,toC,toR,toC,0,nextValue);
    [len2,j2] = checkLeft(B,currR,currC,currR,toC,1,nextValue);
    jump = j1+j2;
end;

% MOVE 25
function [len1,len2,jump] = checkRightUp(B,currR,currC,toR,toC,nextValue)
len1 = -1; 
len2 = -1;
j1 = 0; j2 = 0; jump = 0;
if (currR ~= toR) && (currC ~= toC) && (currR > 1)
    [len1,j1] = checkUp(B,currR,toC,toR,toC,0,nextValue);
    [len2,j2] = checkRight(B,currR,currC,currR,toC,1,nextValue);
    jump = j1+j2;
end;

% MOVE 26
function [len1,len2,jump] = checkDownLeft(B,currR,currC,toR,toC,nextValue)
len1 = -1; 
len2 = -1;
j1 = 0; j2 = 0; jump = 0;
if (currR ~= toR) && (currC ~= toC) && (currR < size(B,1))
    [len1,j1] = checkDown(B,currR,currC,toR,currC,1,nextValue);
    [len2,j2] = checkLeft(B,toR,currC,toR,toC,0,nextValue);
    jump = j1+j2;
end;

% MOVE 27
function [len1,len2,jump] = checkRightDown(B,currR,currC,toR,toC,nextValue)
len1 = -1; 
len2 = -1;
j1 = 0; j2 = 0; jump = 0;
if (currR ~= toR) && (currC ~= toC) && (currR < size(B,1))
    [len1,j1] = checkDown(B,currR,toC,toR,toC,0,nextValue);
    [len2,j2] = checkRight(B,currR,currC,currR,toC,1,nextValue);
    jump = j1+j2;
end;

% MOVE 28
function [len1,len2,jump] = checkUpLeft(B,currR,currC,toR,toC,nextValue)
len1 = -1; 
len2 = -1;
j1 = 0; j2 = 0; jump = 0;
if (currR ~= toR) && (currC ~= toC) && (currR > 1)
    [len1,j1] = checkUp(B,currR,currC,toR,currC,1,nextValue);
    [len2,j2] = checkLeft(B,toR,currC,toR,toC,0,nextValue);
    jump = j1+j2;
end;