Sudoku Problem Sudokutxt contains a 9 times 9 matrix solutio

Sudoku Problem: Sudoku.txt contains a 9 times 9 matrix solution to a Sudoku problem with values ranging from 1 to 9. The rules of Sudoku are the following: Each row should have the number 1 through 9 (no repeats) Each column should have the number 1 through 9 (no repeats) Each of the nine 3 times 3 squares should have the number 1 through 9 (no repeats) Write a Matlab script that will load in the file, evaluate whether the matrix satisfies the rules stated above, and display yes no depending on the result.

Solution

% sudoku program

function x = sudoku(x)

% c is a cell array
%s1 is the first cell , if any with one candidate
%s2 is the first cell , with no candidates

[c,s1,s2] = candidates(x);
while ~isempty(s1) && isemty(s2)
x(s1) = c{s1};
[c,s1,s2] = candidates(x);
end

% it returns impossible puzzles
if ~isempty(s2)
return
end

% It is for Recursive backtracking function
if any(x(:) == 0)
y=x;
z = find(x(:) == 0,1);
for r = [c{z}]
x = y;
x(z) = r;
x = sudoku(x);
if all(x(:) > 0)
return
end
end
end


function [c,s1,s2] = candidates(x)
c = cell(9,9);
tri = @(k) 3*ceil(k/3-1) + (1:3);
for j = 1:9
for i = 1:9
if x(i,j)==0
z = 1:9;
z(nonzeros(X(i,:))) = 0;
z(nonzeros(X(:,j))) = 0;
z(nonzeros(X(tri(i),tri(j)))) = 0;
c{i,j} = nonzero(z)\';
end
end
end

L = cellfun(@length,c);
s1 = find(x==0 & L==1,1);
s2 = find(x==0 & L==0,1);
end
end

 Sudoku Problem: Sudoku.txt contains a 9 times 9 matrix solution to a Sudoku problem with values ranging from 1 to 9. The rules of Sudoku are the following: Eac
 Sudoku Problem: Sudoku.txt contains a 9 times 9 matrix solution to a Sudoku problem with values ranging from 1 to 9. The rules of Sudoku are the following: Eac

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site