Question

Problem 2 Sudoku is a logic-puzzle game played on a 9 x 9 grid, which is also sub-divided into 9 smaller grids of size 3x3 ca

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, 9, 2, 6, 8, 3},
                        {6, 9, 4, 7, 3, 8, 2, 1, 5},
                        {3, 2, 8, 5, 6, 1, 7, 4, 9}};
    assert(checkSudoku(sudoku)==true);
    sudoku[0][0] = 0;
    assert(checkSudoku(sudoku)==true);
    sudoku[0][1] = 0;
    assert(checkSudoku(sudoku)==true);
    sudoku[0][0] = 8;
    assert(checkSudoku(sudoku)==false);
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdbool.h>
#include <assert.h>


bool checkSudoku(int sudoku[9][9])
{
//check each row
for(int i = 0;i<9;i++){
int unq[10] ={0};
for(int j = 0; j<9;j++){
if(sudoku[i][j] ==0){
continue;
}
if(unq[sudoku[i][j]] != 0) return false;
else unq[sudoku[i][j]]++;
}
  
}
  
//check each column
for(int i = 0;i<9;i++){
int unq[10] ={0};
for(int j = 0; j<9;j++){
if(sudoku[j][i] ==0) continue;
  
if(unq[sudoku[j][i]] != 0) return false;
else unq[sudoku[j][i]]++;
}
  
}
  
//check each sub-box
  
int x = 0 , y = 0;
  
while(x<9 && y<9){
int unq[10] = {0};
for(int i = x;i<x+3;i++ )
for(int j = y;j<y+3;j++){
if(sudoku[i][j] ==0) continue;
  
if(unq[sudoku[i][j]] != 0) return false;
else unq[sudoku[i][j]]++;
}
  
x += 3;
if(x == 9 && y<6){
x = 0;
y +=3;
}
}
return true;
}


//square boxes checking

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, 9, 2, 6, 8, 3},
{6, 9, 4, 7, 3, 8, 2, 1, 5},
{3, 2, 8, 5, 6, 1, 7, 4, 9}};
assert(checkSudoku(sudoku)==true);
sudoku[0][0] = 0;
assert(checkSudoku(sudoku)==true);
sudoku[0][1] = 0;
assert(checkSudoku(sudoku)==true);
sudoku[0][0] = 8;
assert(checkSudoku(sudoku)==false);
}

л bool checkSudoku (int sudoku[9] [9]) о р о о //check each row for (int i = 0;i<9;i++) { int ung[10] ={0}; for(int j = 0; j<- x D C:\Users\Pankaj\Desktop\ct.cpp - Notepad++ File Edit Search View Encoding Language Settings Tools Macro Run Plugins Win

Add a comment
Know the answer?
Add Answer to:
Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here }...
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
  • 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...

  • Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as...

    Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...

  • Python Code. Sudoku is a puzzle where you're given a partially-filled 9 by 9 grid with...

    Python Code. Sudoku is a puzzle where you're given a partially-filled 9 by 9 grid with digits. The objective is to fill the grid with the constraint that every row, column, and box (3 by 3 subgrid) must contain all of the digits from 1 to 9. Implement an efficient sudoku solver.

  • JAVA PROJECT Sudoku is a popular logic puzzle that uses a 9 by 9 array of...

    JAVA PROJECT Sudoku is a popular logic puzzle that uses a 9 by 9 array of squares that are organized into 3 by 3 subarrays. The puzzle solver must fill in the squares with the digits 1 to 9 such that no digit is repeated in any row, any column, or any of the nine 3 by 3 subgroups of squares. Initially, some squares are filled in already and cannot be changed. For example, the following might be a starting...

  • JAVA Sudoku.java This class does the work of solving the Sudoku puzzle, as well as containing...

    JAVA Sudoku.java This class does the work of solving the Sudoku puzzle, as well as containing the main method to provide a text-based user interface. Stores a Board object which it uses in solving. It should also track statistics about the solving process: the number of recursive calls made, and the number of "backups" that had to be done. public Sudoku( Scanner sc ) Constructor for the Sudoku class. Initializes the board and other instance variables. public boolean solve( Location...

  • 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++)...

  • 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'm making a sudoku solver to check if the sudoku grid created is legal. There are...

    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 //...

  • C language Credit for work. Please design a multithreaded application in C with Pthreads - it...

    C language Credit for work. Please design a multithreaded application in C with Pthreads - it determines whether the solution to a Sudoku puzzle is valid. Validate two puzzles In your program, please hard-code the following two 9x9 grids (say in two variables puzzle1 and puzzle2), and determine if each solution is valid. While each grid should be validated by multiple parallel threads, you can validate puzzle1 and then puzzle2 in sequential order (as illustrated) by a single invocation of...

  • 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...

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