function W = solver(B)
W = zeros(0,4);
[II,JJ,AA] = find (B);
A = [II JJ AA];
Points = [A(:,1:2) A(:,1:2)];
Alfab = unique (B);
Alfab = Alfab (find(Alfab));
Score = zeros (size(Alfab));
BestScore = 0;
for ix = 1:length (Alfab)
Value = Alfab(ix);
findV = find (A(:,3)==Value);
PV = A (findV, 1:2);
Path = TheBest (PV);
Score(ix) = size (Path,1) + sum(sum(B)) - 2*Value;
if Score(ix) < BestScore
W = Path;
BestScore = Score(ix);
end
end
return;
function Best = TheBest (PV)
if size(PV,1) > 1
Diffe = PV(2,:) - PV(1,:);
Best = zeros (sum(abs(Diffe)), 4);
for iy = 1:abs(Diffe(1))
Best (iy, :) = [PV(1,1)+sign(Diffe(1))*iy PV(1,2)];
end
for iz = iy+1:size(Best,1)
Best (iz, :) = [PV(2,1) PV(1,2)+sign(Diffe(2))*(iz-iy)];
end
else Best = zeros (0,4);
return;
|