function [moves, vine] = solver(board, limit)
%% zonder moves
moves = [];
[bestsom,bestvine] = max(board(:));
A=board;
[m,n]=size(board);
inhoud = unique(A(:))';
heur = sparse(0);
for i=inhoud;
heur(i) = sum(sum(A(A<=i)));
end
H=full(heur(A));
B=A;
IND = reshape(1:m*n,[m,n]);
C = num2cell(IND);
Hmod = zeros(m,n);
updated = true(size(A));
iteration = 0;
while 1
G=B+H;
G=G.*updated;
[maxg,startindex] = max(G(:));
[bestsom] = max(B(:));
if maxg<=bestsom
break
end
if maxg==0
break
end
iteration = iteration+1;
i = mod(startindex,m);
j = ceil(startindex/m);
if i==0
i=m;
end
if 1<i && A(i-1,j)<=A(i,j)
verplaatsindex = IND(i-1,j);
if ~any(verplaatsindex==C{i,j})
[B(verplaatsindex),which] = max([B(verplaatsindex),B(i,j)+A(verplaatsindex)]);
if which == 2
updated(verplaatsindex)=true;
C{verplaatsindex} = [C{i,j} verplaatsindex];
% Hmod(verplaatsindex) = sum(A(C{verplaatsindex})==A(verplaatsindex));
end
end
end
if i<size(A,1) && A(i+1,j)<=A(i,j)
verplaatsindex = IND(i+1,j);
if ~any(verplaatsindex==C{i,j})
[B(verplaatsindex),which] = max([B(verplaatsindex),B(i,j)+A(verplaatsindex)]);
if which == 2
updated(verplaatsindex)=true;
C{verplaatsindex} = [C{i,j} verplaatsindex];
% Hmod(verplaatsindex) = sum(A(C{verplaatsindex})==A(verplaatsindex));
end
end
end
if j<size(A,2) && A(i,j+1)<=A(i,j)
verplaatsindex = IND(i,j+1);
if ~any(verplaatsindex==C{i,j})
[B(verplaatsindex),which] = max([B(verplaatsindex),B(i,j)+A(verplaatsindex)]);
if which == 2
updated(verplaatsindex)=true;
C{verplaatsindex} = [C{i,j} verplaatsindex];
% Hmod(verplaatsindex) = sum(A(C{verplaatsindex})==A(verplaatsindex));
end
end
end
if 1<j && A(i,j-1)<=A(i,j)
verplaatsindex = IND(i,j-1);
if ~any(verplaatsindex==C{i,j})
[B(verplaatsindex),which] = max([B(verplaatsindex),B(i,j)+A(verplaatsindex)]);
if which == 2
updated(verplaatsindex)=true;
C{verplaatsindex} = [C{i,j} verplaatsindex];
% Hmod(verplaatsindex) = sum(A(C{verplaatsindex})==A(verplaatsindex));
end
end
end
updated(i,j)=false;
end
[som1,index]=max(B(:));
vine1 = fliplr(C{index});
moves1 = [];
%% met moves
[m,n]=size(board);
INDICES = reshape(1:m*n,[m,n]);
k=1;
l=1;
moves = [];
vine = [];
vorig = inf;
while limit>0
[maxboard,currentposition] = max(board(:));
if maxboard == 0
break
end
x = mod(currentposition,m);
y = ceil(currentposition/m);
if x==0
x=m;
end
limit = limit - x-l - y-k;
if limit>=0
if k<=y
%y ligt rechts van k
board(x,k:y) = 0;
ind=y:-1:k+1;
moves = [moves; INDICES(x,ind)' INDICES(x,ind-1)'];
else
%y ligt links van k
board(x,y:k) = 0;
ind=y:+1:k-1;
moves = [moves; INDICES(x,ind)' INDICES(x,ind+1)'];
end
%x ligt onder l l<=x
board(l:x,k)= 0;
ind=x:-1:l+1;
moves = [moves; INDICES(ind,k) INDICES(ind-1,k)];
board(l,k) = -maxboard;
vine = [vine INDICES(l,k)];
vorig = board(l,k);
if mod(l,2) == 1
k=k+1;
else
k=k-1;
end
if k>n
k=n;
l=l+1;
end
if k<1
k=1;
l=l+1;
end
end
end
while 1
if board(l,k)>vorig || board(l,k) == 0
break
end
vine = [vine INDICES(l,k)];
vorig = board(l,k);
if mod(l,2) == 1
k=k+1;
else
k=k-1;
end
if k>n
k=n;
l=l+1;
end
if k<1
k=1;
l=l+1;
end
end
som2 = -sum(board(vine));
moves2 = moves;
vine2 = fliplr(vine);
if som1>som2
vine = vine1;
moves = moves1;
else
vine = vine2;
moves = moves2;
end
end
|