Digital Signal Processing Reference
In-Depth Information
will show just the rst part that checks each row. See the function listing on the
CD-ROM for the checks on the columns and submatrices.
% sudoku_check.m
%
% Check a Su Doku puzzle -
%
make sure each number appears in only one row,
%
column, and 3x3 submatrix.
%
function OK = sudoku_check(puzzle)
% assume the puzzle is OK, until we find otherwise
OK = true;
% Check to see if the puzzle matrix is good
% First, check along the rows. r = row number
for r = 1:9
% Isolate one row of the puzzle
row = puzzle(r, 1:9);
% let n be the number (1..9)
for n = 1:9
% initialize count for this number
num_count = 0;
% examine each column
for c = 1:9
% if the puzzle entry matches the number
% we are looking for, add one to the count.
if (row(c) == n)
num_count = num_count + 1;
end
end
% make sure every number 1..9 appears <= 1 time
if (num_count > 1)
% we found a duplicate in this row
OK = false;
end
end
end
% See sudoku_check.m for the column check
% and the 3x3 submatrix check
Search WWH ::




Custom Search