Question: IN MATLAB I just need help with explaining what this code is actually doing line by line, it's a sudoku solver and I need to

IN MATLAB

I just need help with explaining what this code is actually doing line by line, it's a sudoku solver and I need to modify it but I'm confused on how it works!

Thank you in advance!

S =[1 0 2 0 3 0 4 0 5; 0 6 0 7 0 8 0 9 0; 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0; 6 5 3 1 4 2 9 7 8; 0 0 0 0 0 0 0 0 0; 0 1 0 2 0 3 0 4 0; 5 0 6 0 7 0 8 0 9; 0 0 0 0 0 0 0 0 0]; global diagonal knight noncon diagonal = 0; knight = 0; noncon = 0; solved = sudoku(S) %% function X = sudoku(X) [C,ns] = state(X); while ns~=0 X(ns) = C{ns}; [C,ns] = state(X); end if any(X(:) == 0) T = X; u = find(X(:) == 0,1); for r = [C{u}] X = T; X(u) = r; X = sudoku(X); if all(X(:) > 0) return end end end end function [C,ns] = state(X) C = cell(9,9); for j = 1:9 for i = 1:9 if X(i,j)==0 zC = sees(X(i,:)); zR = sees(X(:,j)); zB = sees(X(box(i),box(j))); zs = [zC;zR;zB]; z = 1:9; for k = 1:9 if any(zs(:,k)==0) z(k) = 0; end end C{i,j} = nonzeros(z)'; end end end ns = 0;

for i = 1:9 for j = 1:9 if length(C{i,j})==1 && X(i,j)==0 ns = 9*(j-1) + i; return end end end end function B = box(n) if n<=3 B = [1 2 3]; elseif n<=6 B = [4 5 6]; else B = [7 8 9]; end end function cand = sees(conf) cand = 1:9; [row,col] = size(conf); for i = 1:row for j = 1:col if conf(i,j)~=0 cand(conf(i,j))=0; end end end end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!