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<9; row++) for (int col-0; col<9; colt+) int num = values[row][col1: for (int otherCol- col+1; otherCol<9; otherCol++) if (num-values[row] [otherCol] return false; /7 Check every column. If you find an illegal column, return false. for (int row-0 ; row<9;row++) for (int col-0; col<9; col++) int num values [row] [col]; for (int otherRow= col+1; otherRow<9; otherRow++) if (numvalues [row] [otherRow]) return false; 166 167 168 169 170 // Check every block. If you find an illegal block, return false // A11 rows/C있s/blocks are legal. return true; 172 173

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**

* 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;

}

Add a comment
Know the answer?
Add Answer to:
I'm making a sudoku solver to check if the sudoku grid created is legal. There are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0...

    need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0 sudoku(int g[][9]); //constructor //Postcondition: grid = g void initializeSudokuGrid(); //Function to prompt the user to specify the numbers of the //partially filled grid. //Postcondition: grid is initialized to the numbers // specified by the user. void initializeSudokuGrid(int g[][9]); //Function to initialize grid to g //Postcondition: grid = g; void printSudokuGrid(); //Function to print the sudoku grid. bool solveSudoku(); //Funtion to solve the sudoku problem....

  • I need help with the following and written in c++ thank you!: 1) replace the 2D...

    I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • A Sudoku puzzle consists of a 9 x9 grid which holds the numbers 1-9 in each...

    A Sudoku puzzle consists of a 9 x9 grid which holds the numbers 1-9 in each space.The objective is to fill the grid with digits so that each column, each row, and each of the nine 3x3 sub-grids that compose the grid contain all of the digits from 1 to 9 (with no repeats). You are to write a recursive method that will construct a valid solution to the puzzle and return that solution. You may construct as many additional...

  • Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here }...

    Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here } int main() { int sudoku[9][9] = {{7, 3, 5, 6, 1, 4, 8, 9, 2}, {8, 4, 2, 9, 7, 3, 5, 6, 1}, {9, 6, 1, 2, 8, 5, 3, 7, 4}, {2, 8, 6, 3, 4, 9, 1, 5, 7}, {4, 1, 3, 8, 5, 7, 9, 2, 6}, {5, 7, 9, 1, 2, 6, 4, 3, 8}, {1, 5, 7, 4,...

  • Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of...

    Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of N-squared. I.E. No nested for loops in rowIsLatin and colIsLatin. Only nested loop allowed in goodSubsquare. public class Sudoku {               public String[][] makeSudoku(String s) {              int SIZE = 9;              int k = 0;              String[][] x = new String[SIZE][SIZE];              for (int i = 0; i < SIZE; i++) {                     for (int j = 0; j < SIZE; j++)...

  • * Your goal in this exercise is to practice recursion and * to see how a...

    * Your goal in this exercise is to practice recursion and * to see how a properly written recursive solution can * take care of fairly complicated tasks with a few lines * of (well thought out) code. * * We will be solving Sudoku puzzles. In case you have never * solved or seen a Sudoku, you can learn more about them * here: * * https://en.wikipedia.org/wiki/Sudoku * * Your task if to write a function that takes an...

  • Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static...

    Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static String solve(char[][] grid) {        // TODO        /*        * 1. Construct a graph using grid        * 2. Use BFS to find shortest path from start to finish        * 3. Return the sequence of moves to get from start to finish        */               // Hardcoded solution to toyTest        return...

  • C PROGRAMMING You must write a C program to check the validity of a Sudoku solution. Follow the link to know more about...

    C PROGRAMMING You must write a C program to check the validity of a Sudoku solution. Follow the link to know more about Sudoku: https://www.bigfishgames.com/blog/how-to-solve-sudoku-puzzles-quickly-andreliably/ You must at least do the following: 1- Ask the user to provide a minimum of first two rows of the Sudoku grid. For the rest of the entries, you should use random number generator. 2- Use appropriate logic to make sure random number generator generates distinct set of valid integers! 3- It should be...

  • USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku...

    USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku game and show its result in detail. This is an application program of using 2-dimensional arrays or lists. Each array or list is a game board of 9 x 9. Your program must check the 9 x 9 game board, and report all the problems among 9 rows, 9 columns, and 9 squares. As you can see, you must pre-load the following four games...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT