Code covered by the BSD License  

Highlights from
Fast Sudoku Solver

image thumbnail
from Fast Sudoku Solver by Yue Wu
An algorithm to mimic human solving procedure for Sudoku puzzles

solve_sudoku_init(I)
function simple = solve_sudoku_init(I)
% Function: solve_sudoku_init
%           used for initially solving 9x9 sudoku puzzle
%           basically I split the whole solving procedure into two stages
%           this function is used for the 1st stage
%
% Input: I = 9x9 sudoku puzzle
%        for example: 
%          I = [0 0 0 3 0 1 7 0 0 ; 
%               1 0 0 0 4 0 0 2 0 ;
%               0 6 0 0 0 0 0 5 0 ;
%               9 0 0 4 0 5 0 0 0 ;
%               0 0 6 0 0 0 1 0 0 ;
%               0 0 0 2 0 6 0 8 9 ;
%               0 7 0 0 0 0 4 9 0 ;
%               0 8 0 0 5 0 0 0 2 ;
%               0 0 9 0 0 7 0 0 0 ;]
% Output: simplifier = 9x9 initial solution leaving unsolved grids zeros
%         for example:   
%           simplifier = [0 0 0 3 0 1 7 0 0 ;
%                         1 0 0 5 4 0 0 2 0 ;
%                         0 6 0 0 0 0 0 5 1 ;
%                         9 0 0 4 0 5 2 0 0 ;
%                         0 0 6 0 0 0 1 0 0 ;
%                         0 0 0 2 0 6 0 8 9 ;
%                         0 7 0 0 0 0 4 9 0 ;
%                         0 8 0 0 5 4 0 7 2 ;
%                         0 0 9 0 0 7 0 0 0 ;]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is a sub-function of the main function sudoku_yue
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% All copyrights reserved
% By Yue Wu
% 10/05/2009
% ECE Dept, Tufts Univ.
% MA, Medford 02155
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
done = 0;
pre = I;
count = 1;
while ~done
    simple = sudoku_single(pre);
    ind = find(simple==0); %
    test = pre == simple;
    if isempty(ind) == 1 || sum(test(:)) == 81
        done = 1;
    end
    pre = simple;
%     display(['iteration ' num2str(count)]);
%     display(simple);
    count = count+1;
end

Contact us at files@mathworks.com