Sudokutxt contains a 9 times 9 matrix solution to a Sudoku p

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 3x3 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

check.m (File to do the checking)

function iscorrect = check(mat)
%row check
for j=1:9
    vals = zeros(9);
    for i=1:9
        vals(i) = mat(i,j);
        if vals(i)<=0 || vals(i)>9
            iscorrect = false;
        end
    end
    for p=1:9
        for q=1:9
            if (p~=q)
                if vals(p) == vals(q)
                    iscorrect = false;
                end
            end
        end
    end
end

%column check
for j=1:9
    vals = zeros(9);
    for i=1:9
        vals(i) = mat(j,i);
        if vals(i)<=0 || vals(i)>9
            iscorrect = false;
        end
    end
    for p=1:9
        for q=1:9
            if (p~=q)
                if vals(p) == vals(q)
                    iscorrect = false;
                end
            end
        end
    end
end

%block check
for j=1:9
    vals = zeros(3,3);
    for i=1:9
        vals(mod(i,3)+1,mod(j,3)+1) = mat(i,j);
        if vals(mod(i,3)+1,mod(j,3)+1)<=0 || vals(mod(i,3)+1,mod(j,3)+1)>9
            iscorrect = false;
        end
    end
    for p=1:3
        for q=1:3
            for r=1:3
                for s=1:3
                    if (p~=r && q~=s)
                        if vals(p, q) == vals(r, s)
                            iscorrect = false;
                        end
                    end
                end
            end
        end
    end
end

iscorrect = true;

sudoku.m (File to load the sudoku and run the script)

sud = load(\'Sudoku.txt\');
mat = zeros(9,9);
for i=1:9
    for j=1:9
        mat(j,i) = sud(9*(i-1)+j);
    end
end
if check(mat)==true
    disp(\'The sudoku is correctly solved.\');
else
    disp(\'Incorrect\');
end

 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 hav
 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 hav

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site