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

sudoku_single(I)
function simplifier = sudoku_single(I)

% Function: sudoku_single
%           used for initially solving 9x9 sudoku puzzle         
%
% 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 sub-function sudoku_init
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% All copyrights reserved
% By Yue Wu
% 10/05/2009
% ECE Dept, Tufts Univ.
% MA, Medford 02155
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% 1. Initial settings
simplifier = I;

%% 2. Retrieve existent information
for i = 1:9
    ori_row{i} = setdiff(unique(I(i,:)),0); % existent information of each row
    ori_col{i} = setdiff(unique(I(:,i)),0); % existent information of each column
end

% existent information of each block
list = [1 4 7]; % row/col number list for each block
for x = 1:3 % block number(horizontal)
    for y = 1:3 % block number (vertical)
        i = list(x);
        j = list(y);
        ori_boc{x,y} = setdiff(unique(I(i:i+2,j:j+2)),0); % existent information of each block
    end
end

%% 3. Find possible fill-in number for each grid in sudoku
possi = sudoku_grid_candidates(I); 

%% 4. Logical analysis
% get the numbers might be used for each row, column or block
[row,col,boc] = uniquelize_sudoku(possi);
% find the numbers only appears once for each row, col and boc
uni_row = sudoku_lowmode(row);
uni_col = sudoku_lowmode(col);
uni_boc = sudoku_lowmode(boc);

% calcuate the numbers could be filled for each row, col and boc
for i = 1:9
    sel_row{i} = setdiff(uni_row{i},ori_row{i});
    sel_col{i} = setdiff(uni_col{i},ori_col{i});
    sel_boc{i} = setdiff(uni_boc{i},ori_boc{i});
end

%% 5. fill the numbers that appear once
% fill the numbers that appear once for each row
out_row = fill_unique(sel_row,possi,'row');
ind_row = find(out_row~=0);
simplifier(ind_row) = out_row(ind_row);
% fill the numbers that appear once for each col
out_col = fill_unique(sel_col,possi,'col');
ind_col = setdiff(find(out_col~=0),ind_row);
simplifier(ind_col) = out_col(ind_col);
% fill the numbers that appear once for each boc
out_boc = fill_unique(sel_boc,possi,'boc');
ind_boc = setdiff(find(out_boc~=0),union(ind_row,ind_col));
simplifier(ind_boc) = out_boc(ind_boc);
        
            

Contact us at files@mathworks.com