B
blux
I am working on a function to check the validity of a sudoku puzzle.
It must check the 9x9 matrix to make sure it follows the rules and is
a valid sudoku puzzle.
this is what I have come up with so far:
However I have found that it does not check it correctly.
I just need to check the 9x9 array, which I am passing to this
function against the classic sudoku rules and then return true for
false.
Any ideas?
/** Check whether grid[j] is valid in the grid */
bool isValid(int grid[] [9])
{
int i, j;
bool status;
status = true;
for (int column = 0; column < 9; column++)
if (column != j && grid [column] == grid [j])
status = false;
for (int row = 0; row < 9; row++)
if (row != i && grid[row] [j] == grid [j])
status = false;
for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
if (row != i && col != j && grid[row] [col] == grid [j])
status = false;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[j] != 0)
status = false;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if ((grid[j] < 0) || (grid[j] > 9))
status = false;
return status;
}
It must check the 9x9 matrix to make sure it follows the rules and is
a valid sudoku puzzle.
this is what I have come up with so far:
However I have found that it does not check it correctly.
I just need to check the 9x9 array, which I am passing to this
function against the classic sudoku rules and then return true for
false.
Any ideas?
/** Check whether grid[j] is valid in the grid */
bool isValid(int grid[] [9])
{
int i, j;
bool status;
status = true;
for (int column = 0; column < 9; column++)
if (column != j && grid [column] == grid [j])
status = false;
for (int row = 0; row < 9; row++)
if (row != i && grid[row] [j] == grid [j])
status = false;
for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
if (row != i && col != j && grid[row] [col] == grid [j])
status = false;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[j] != 0)
status = false;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if ((grid[j] < 0) || (grid[j] > 9))
status = false;
return status;
}