Borrar filtros
Borrar filtros

Why is the output value a zero when it should be 1 for this program?

2 visualizaciones (últimos 30 días)
This function takes a matrix representing a 4x4 Sudoku board and returns true if the board is a valid Sudoku solution and false if it is not. The conditions for a valid board are: the numbers on the board should only be one of 1,2,3,4, also a number can not not be repeated on a row or column of the board or within any of the four 2x2 sub-blocks. Here is my code:
function out=issudoku(m)
[R,C]=size(m);
%Find if any number on the board is not a 1, 2, 3 or 4
for i=1:R
for j=1:C
if m(i,j)~=1||2||3||4
out=0;
%%%see if any number is repeated within the individual squares
elseif m(1,1)==m(2,1)||m(1,2)||m(2,2)
out=0;
elseif m(3,1)==m(4,1)||m(3,2)||m(4,4)
out=0;
elseif m(1,3)==m(2,3)||m(1,4)||m(2,4)
out=0;
elseif m(3,3)==m(4,3)||m(3,4)||m(4,4)
out=0;
%%repeated numbers in columns
elseif any(diff(m(:,1)==0))==1
out=0;
elseif any(diff(m(:,2)==0))==1
out=0;
elseif any(diff(m(:,3)==0))==1
out=0;
elseif any(diff(m(:,4)==0))==1
out=0;
%repeated numbers within rows:
elseif any(diff(m(1,:)==0))==1
out=0;
elseif any(diff(m(2,:)==0))==1
out=0;
elseif any(diff(m(3,:)==0))==1
out=0;
elseif any(diff(m(4,:)==0))==1
out=0;
else
out=1;
end
end
end
When i run issudoku([1 4 2 3; 3 2 4 1; 1 4 3 2; 2 3 1 4]) in the command window I am getting a 0 as a result when it should be 1.

Respuesta aceptada

Jan
Jan el 5 de Mayo de 2016
The line
if m(i,j)~=1||2||3||4
does not perform, what you expect. The condtion is evaluated from right to left:
1. m(i,j)~=1 ==> replies e.g. TRUE
2. (m(i,j)~=1) || 2 ==> this is TRUE || 2, which is TRUE in every case
3. TRUE || 3 ==> This is TRUE also
4. TRUE || 4 ==> guess
I assume you want:
if ~isember(m(i,j), 1:4)
or another formulation:
if m(i,j) > 4
or
if m(i,j)~=1 && m(i,j)~=2 && m(i,j)~=3 && m(i,j)~=4

Más respuestas (2)

Steven Lord
Steven Lord el 5 de Mayo de 2016
In addition to what Jan said, the matrix you're testing is not a valid 4-by-4 Sudoku solution. Column 1 contains two 1's and no 4's, and column 2 contains two 4's and no 1's. Swapping the first two elements of either row 1 or row 3 will make it a valid Sudoku solution. I'd recommend swapping row 3 as then each of the main diagonal and antidiagonal will contain the digits 1 through 4 once as well.
[1 4 2 3;
3 2 4 1;
4 1 3 2;
2 3 1 4]
  2 comentarios
Mohannad Abboushi
Mohannad Abboushi el 5 de Mayo de 2016
It is a valid Sudoku matrix, since all the conditions are satisfied in:
[1 4 2 3; 3 2 4 1; 1 4 3 2; 2 3 1 4]
Jan
Jan el 6 de Mayo de 2016
But there are several 1s in the first column?!

Iniciar sesión para comentar.


Jan
Jan el 6 de Mayo de 2016
Another problem:
elseif any(diff(m(1,:)==0))==1
What do you assume does this condition do?
m(1,:)==0 compares all elements of the 1st row with 0. Because the matrix contains the values 1 to 4, this is [true, true, true, true] in every case. Then you perform a diff. Because all elements are equal, this replies [0,0,0] in every case. Then any([0,0,0])==1 replies false. Note, that you can omit the "==1" part.
You see, that all these elseif any(... commands are not meaningful also.
Please fix this problem also and post your code again, such that we can search for further errors.
  1 comentario
Mohannad Abboushi
Mohannad Abboushi el 6 de Mayo de 2016
Here's my new code. When I put a matrix that has duplicate elements within its columns i.e. [1 4 2 3; 3 2 4 1; 1 4 3 2; 2 3 1 4]. It is still keeping out as true. While debugging it, I saw that it skipped assigning out as false after if a~=validnumbers.
function out=issudoku(m)
out=true;
validnumbers=[1,2,3,4];
[R,C]=size(m);
%check if sudoku board is 4x4
if R~=4||C~=4
out=false;
end
%check if any elements are greater than 4
if any(m>4)
out=false;
end
%check for any repeat values in columns
for j=1:C
a=m(:,j);
a=sort(a');
if a~=validnumbers
out=false;
end
end
%check for any repeat values in rows
for i=1:R
b=m(i,:);
sort(b);
if b~=validnumbers
out=false;
end
end
%check if any number is repeated within the individual squares
submatrix1=m([1 2],[1 2]);
if sort(reshape(submatrix1,1,[]))~=[1 2 3 4]
out=false;
end
submatrix2=m([3 4],[1 2]);
if sort(reshape(submatrix2,1,[]))~=[1 2 3 4]
out=false;
end
submatrix3=m([1 2],[3 4]);
if sort(reshape(submatrix3,1,[]))~=[1 2 3 4]
out=false;
end
submatrix4=m([3 4],[3 4]);
if sort(reshape(submatrix4,1,[]))~=[1 2 3 4]
out=false;
end

Iniciar sesión para comentar.

Categorías

Más información sobre Sudoku en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by