from
MATLAB Contest - Peg Solitaire
by The MATLAB Contest Team
All the files needed to develop and score an entry for the MATLABĀ® Programming Contest.
|
| solver(board)
|
function moves = solver(board)
% MOVES = SOLVER(BOARD) Solitaire solver.
%
% MOVES -> [row_from,column_from,row_to,column_to]
% Copyright 2007 The MathWorks, Inc.
[m,n] = size(board);
s = @(i,j) sub2ind([m,n],i,j);
[i,j] = find(board>0);
I = [i;i;i;i];
J = [j;j;j;j];
K = [i;i;i-2;i+2];
L = [j-2;j+2;j;j];
h = find(K>0 & K<=m & L>0 & L<=n);
h = h(board(s(K(h),L(h)))==0 & board(s((K(h)+I(h))/2,(L(h)+J(h))/2))>0);
moves = [I(h(1)) J(h(1)) K(h(1)) L(h(1))];
|
|
Contact us at files@mathworks.com