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

fill_unique(sel_cell,candi,keyword)
function output = fill_unique(sel_cell,candi,keyword)
% Function: fill_unique
%           used for filling number in sudoku_stage1
%           
% Input: sel_cell = the number could be filled in
%        candi = 9x9 cell (candidats cell)
%                whose elements are possible numbers for its corresponding grid
%        For example:
%        candi = {[2,4,5,8],[2,4,5,9],[2,4,5,8],3,[2,6,8,9],1,7,[4,6],[4,6,8];
%                 1,[3,5,9],[3,5,7,8],[5,6,7,8,9],4,[8,9],[3,6,8,9],2,[3,6,8];
%                 [2,3,4,7,8],6,[2,3,4,7,8],[7,8,9],[2,7,8,9],[2,8,9],[3,8,9],5,[1,3,4,8];
%                 9,[1,2,3],[1,2,3,7,8],4,[1,3,7,8],5,[2,3,6],[3,6,7],[3,6,7];
%                 [2,3,4,5,7,8],[2,3,4,5],6,[7,8,9],[3,7,8,9],[3,8,9],1,[3,4,7],[3,4,5,7];
%                 [3,4,5,7],[1,3,4,5],[1,3,4,5,7],2,[1,3,7],6,[3,5],8,9;
%                 [2,3,5,6],7,[1,2,3,5],[1,6,8],[1,2,3,6,8],[2,3,8],4,9,[1,3,5,6,8];
%                 [3,4,6],8,[1,3,4],[1,6,9],5,[3,4,9],[3,6],[1,3,6,7],2;
%                 [2,3,4,5,6],[1,2,3,4,5],9,[1,6,8],[1,2,3,6,8],7,[3,5,6,8],[1,3,6],[1,3,5,6,8];}
%         keyword = 'row', 'col', or 'boc'
% Output: output = 9x9 matrix with only filled numbers nonzeros 
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is a sub-function of the sub-function sudoku_stage1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% All copyrights reserved
% By Yue Wu
% 10/05/2009
% ECE Dept, Tufts Univ.
% MA, Medford 02155
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% initialization
output = zeros(9); 
[lookx,looky] = meshgrid(1:3);

for i = 1:9
    if ~isempty(sel_cell{i}) % possible number exists
        for j = 1:9
            % according to different keyword get the information from candi
            switch keyword 
                case 'row'
                    tmp = candi{i,j}; 
                case 'col'
                    tmp = candi{j,i};
                case 'boc'
                    [x,y] = ind2sub([3,3],i);
                    cx = (x-1)*3+lookx(j);
                    cy = (y-1)*3+looky(j);
                    tmp = candi{cx,cy};
            end
            
            inter = intersect(sel_cell{i},tmp); 
            if ~isempty(inter) % if current grid contains the number just appears once
                % according to its keyword, filled in the number in output matrix
                switch keyword
                    case 'row'
                        output(i,j) = inter;
                    case 'col'
                        output(j,i) = inter;
                    case 'boc'
                        output(cx,cy) = inter;
                end
            end
        end
    end
end

                        

Contact us at files@mathworks.com