function [moves, vine] = solverX(board, limit)
% The MATLAB Contest Team
% Copyright 2011 The MathWorks, Inc.
[m,n] = size(board);
vine = zeros(1,m*n);
TempBrd = board;
idx = 1;
%vine(1) = idx;
k = 0;
values = 1;
l = 1;
while ~isequal( values, -ones(1,l) )
k = k+1;
vine(k) = idx;
TempBrd = updateBoard(TempBrd,idx);
if idx == 1 || idx == m*n-m+1 || idx == m || idx == m*n
nbrs = neighbors1(m,n,idx);
else
nbrs = neighbors2(m,n,idx);
end
values = TempBrd(nbrs);
[~,L] = size(nbrs);
U = random('unid',L);
idx = nbrs(U);
end
vine = vine(1:k);
moves = [];
end
function NewBrd = updateBoard(OldBrd,idx)
NewBrd = OldBrd;
NewBrd(idx) = -1;
end
function nbhd = neighbors1(m,n,idx)
if idx == 1
nbhd = zeros(1,2);
nbhd(1) = idx+1;
nbhd(2) = idx+m;
else if idx == m*n-m+1
nbhd = zeros(1,2);
nbhd(1) = idx+1;
nbhd(2) = idx-m;
else if idx == m
nbhd = zeros(1,2);
nbhd(1) = idx-1;
nbhd(2) = idx+m;
else
nbhd = zeros(1,2);
nbhd(1) = idx-1;
nbhd(2) = idx-m;
end
end
end
end
function nbhd = neighbors2(m,n,idx)
if mod(idx,m) == 1 && idx ~= 1 && idx ~= m*n-m+1
nbhd = zeros(1,3);
nbhd(1) = idx+1;
nbhd(2) = idx-m;
nbhd(3) = idx+m;
else if mod(idx,m) == 0 && idx ~= m && idx ~= m*n
nbhd = zeros(1,3);
nbhd(1) = idx-1;
nbhd(2) = idx-m;
nbhd(3) = idx+m;
else if 1 < idx && idx < m
nbhd = zeros(1,3);
nbhd(1) = idx-1;
nbhd(2) = idx+1;
nbhd(3) = idx+m;
else if m*n-m+1 < idx && idx < m*n
nbhd = zeros(1,3);
nbhd(1) = idx-1;
nbhd(2) = idx+1;
nbhd(3) = idx-m;
else
nbhd = zeros(1,4);
nbhd(1) = idx-1;
nbhd(2) = idx+1;
nbhd(3) = idx-m;
nbhd(4) = idx+m;
end
end
end
end
end
|