i would like the C code for this please Dave has become addicted to Sudoku, the
ID: 3663238 • Letter: I
Question
i would like the C code for this please
Dave has become addicted to Sudoku, the latest puzzle craze in all the newspapers and
bookstands. In case you don’t know, a Sudoku is a simple number puzzle played on a 3x3 grid of
3x3 subgrids. Below is an example
The object of Sudoku is to place numbers 1 through 9 in the empty spaces such that no row,
column, or 3x3 subgrid has any number more than once. An interesting property of Sudoku
puzzles is that there is always only one possible solution, and it can always be determined using
logic, without the need for guessing. Although Dave is wild about Sudoku, he still comes up with
incorrect solutions. Dave is tired of being made fun of by his more Sudoku savvy friends, so he’s
asked you to write a program to check his solutions for him.
Dave would like to give you his completed Sudoku puzzle solutions to have you determine
which ones are correct, and which are invalid. For a Sudoku solution to be correct, every row,
column, and 3x3 subgrid of the puzzle must have each digit (1 through 9) exactly once.
Input Format (Input will be tested from standard input)
Dave will give you multiple solutions to check. The input will begin with a single integer, n, on a
line by itself. Following this will be n sets of 9 lines, each containing 9 digits. Each of these
digits will be in the range 1 through 9, inclusive. Each set of 9 lines of 9 digits represents one of
Dave’s potential Sudoku puzzle solutions
Output Specification
For each Sudoku solution, print a single line with either the word "YES" or the word "NO".
Sample Input
Explanation / Answer
Here is the code for you:
#include <stdio.h>
void readSudoku(int Sudoku[][9])
{
printf("Enter the 9x9 Sudoku puzzle: ");
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
scanf("%i", &Sudoku[i][j]);
}
int countDigitInRows(int Sudoku[][9], int x, int rowNum)
{
int count = 0;
for(int i = 0; i < 10; i++)
if(Sudoku[rowNum][i] == x)
count++;
return count;
}
int countDigitInCols(int Sudoku[][9], int x, int colNum)
{
int count = 0;
for(int i = 0; i < 10; i++)
if(Sudoku[i][colNum] == x)
count++;
return count;
}
int countDigitInGrids(int Sudoku[][9], int x, int rowNum, int colNum)
{
int count = 0;
for(int i = rowNum; i < rowNum+3; i++)
for(int j = colNum; j < colNum+3; j++)
if(Sudoku[i][j] == x)
count++;
return count;
}
int ValidateSudoku(int Sudoku[][9])
{
for(int rowNum = 0; rowNum < 9; rowNum++) //For every row.
for(int x = 1; x < 10; x++) //For every value from 1 to 9.
if(countDigitInRows(Sudoku, x, rowNum) != 1) //If the number of times the value x appears in that row is not 1.
return 0; //Its not a valid puzzle.
for(int colNum = 0; colNum < 9; colNum++) //For every column.
for(int x = 1; x < 10; x++) //For every value from 1 to 9.
if(countDigitInCols(Sudoku, x, colNum) > 1) //If the number of times the value x appears in that column is not 1.
return 0; //Its not a valid puzzle.
for(int x = 1; x < 10; x++) //For every value from 1 to 9.
for(int rowNum = 0; rowNum < 9; rowNum += 3) //For every grid.
for(int colNum = 0; colNum < 9; colNum += 3) //For every grid.
if(countDigitInGrids(Sudoku, x, rowNum, colNum) > 1) //If the number of times the value x appears in that grid is not 1.
return 0; //Its not a valid puzzle.
return 1; //Its a VALID puzzle.
}
int main()
{
int Sudoku[9][9], n;
printf("Enter the number of puzzle solutions you want to enter: ");
scanf("%i", &n);
for(int i = 0; i < n; i++)
{
readSudoku(Sudoku);
if(ValidateSudoku(Sudoku))
{
printf("YES. ");
break;
}
else
printf("NO. ");
}
}
If you have any further queries, just get back to me.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.