function mv = solver(ai,af,w)
totw = Inf;
for iter = 1:20
[mv2,totw2]=solver2(ai,af,w);
if totw2 < totw
mv = mv2;
totw = totw2;
end
end
function [mv,totw] = solver2(ai,af,w)
nBlocks = max(ai(:));
[m,n] = size(ai);
% Make increment tables
% N=1, E=2, S=3, W=4
I = [0 1 0 -1];
J = [1 0 -1 0];
a = ai;
mv = [];
while ~isequal(af,a)
% Pick a random block
bid = ceil(rand*nBlocks);
[i,j] = find(a==bid);
% Move it in a random direction
r = ceil(rand*4);
ni = i + I(r);
nj = j + J(r);
% Is it a legal move? Check edges
if (ni<1) || (ni>m) || (nj<1) || (nj>n)
continue
end
% Is it a legal move? Check for collision
if a(ni,nj)>0
continue
end
% Check distance
% Get the target location
[ti,tj] = find(af==bid);
d = (ti-i)^2 + (tj-j)^2;
dn = (ti-ni)^2 + (tj-nj)^2;
% Have we moved closer to the target location?
% allow "worsening" moves a percentage of the time
if (d<dn) && (rand>0.1)
continue
end
% Move the block
a(ni,nj) = bid;
a(i,j) = 0;
% Record the move
mv(end+1,[1 2]) = [bid r];
end
% score the set of moves
totw = sum(w(mv(:,1)));
return
|