Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m making a sudoku solver to check if the sudoku grid created is legal. There

ID: 3595647 • Letter: I

Question

I'm making a sudoku solver to check if the sudoku grid created is legal. There are supposed to be no repeated numbers in each row, column, and block. I got the code for the repeated numbers for row and column but confused on how to write the code for the blocks. Here is my code:

134 135 136 / COMPLETE THIS 137 // Returns true if this grid is legal. A grid is legal if no row, column, or // 3x3 block contains a repeated 1, 2, 3, 4, 5, 6, 7, 8, or 9 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 public boolean isLegal(.O // Check every row. If you find an illegal row, return false for (int row=0 ; row

Explanation / Answer

/**

* Checks whether a block contains duplicate values.

* Returns true if the block grid with upper left corner at [startBlockRow, startBlockCol], does not contain

* any duplicate values.

* @param block

* the 2D array block to check

* @param startBlockRow

* TopMost row of the square

* @param startBlockCol

* LeftMost column of the square

* @param blockSize

* the size of the block to be checked

* @return

* true if the block contains no duplicates, false otherwise

*/

private static boolean checkBlock(int[][] block, int startBlockRow, int startBlockCol, int blockSize)

{

boolean[] found = new boolean[block.length];

for (int row = startBlockRow; row < (startBlockRow + blockSize); ++row)

{

for (int col = startBlockCol; col < (startBlockCol + blockSize); ++col)

{

// set found[x - 1] to be true if we find x in the row

int num = block[row][col] - 1;

if (!found[num])

{

found[num] = true;

}

else

{

// found it twice, so return false

return false;

}

}

}

  

return true;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote