Winner GUO (asdf1)

Finish 2004-11-10 09:00:00 UTC

Bland and Random

by Lesley Northam

Status: Failed
Results:

Comments
Lesley Northam
03 Nov 2004
Tiny mod on sample.
Please login or create a profile.
Code
function mv = solver(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 ( ai, af )
	
	% pick a random block
	block = ceil ( rand * nBlocks );

	% where is this block located
	[ brow, bcol ] = find ( a == block );
	
	% where should the block go?
	[ erow, ecol ] = find ( af == block );

	% how far away are we?
	rdist = abs ( brow - erow );
	cdist = abs ( bcol - ecol );

	% is the block in its correct spot?
	if ( brow ~= erow | bcol ~= ecol )
		
		% randomly pick a direction to move the block
		direction = ceil ( rand * 4 );
		
		% move it in that direction . . .
		nrow = brow + I ( direction );
		ncol = bcol + J ( direction );
		
		% was it a valid move?
		if ( nrow < 1 | nrow > m | ncol < 1 | ncol > n | a ( nrow, ncol ) ~= 0 )
			continue;
		end

		% did we get closer to the goal?
		nrdist = abs ( nrow - erow );
		ncdist = abs ( ncol - ecol );

		if ( nrdist > rdist  | ncdist > cdist )
			continue;
		end

		% it was a valid move!
		a ( nrow, ncol ) = block;
		a ( brow, bcol ) = 0;
		
		mv ( end + 1, [ 1 2 ] ) = [ block direction ];
	else
		continue;
	end
	
end