* 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 input * Sudoku in the form of a 9x9 array of integers. the * array will have a few entries filled with numbers in * [1, 9], and the remaining entries set to zero. * * For the example in the wiki, the input array will * be * * 5 3 0 0 7 0 0 0 0 * 6 0 0 1 9 5 0 0 0 * 0 9 8 0 0 0 0 6 0 * 8 0 0 0 6 0 0 0 3 * 4 0 0 8 0 3 0 0 1 * 7 0 0 0 2 0 0 0 6 * 0 6 0 0 0 0 2 8 0 * 0 0 0 4 1 9 0 0 5 * 0 0 0 0 8 0 0 7 9 * * Your code should find the values that correctly * complete the Sudoku. * * As a brief reminder, a correctly solved Sudoku is * one in which there are no repeated digits in any * row, any column, or any of the 3x3 sub-arrays that * make up the large 9x9 grid. Another way to think * about it is, every row, column, and 3x3 subgrid * uses all of the different digits. * * How to solve this? * * * RECURSIVELY! * * (hint) - Solving a Sudoku with N unknown values * may involve trying something, then solving an * easier Sudoku problem. * * * How to approach this? * * - As noted in class: * a) Determine the base case(s) * b) Figure out the recursive case * c) Determine what needs to happen to * the solution for the entire problem * is returned once the recursion hits * the base case(s) * * - Conditions on the input Sudoku * - It will have AT LEAST ONE DIGIT in each * 3x3 subgrid. Likely there will be several * 3x3 subgrids with several digits already * set to some value. * * - Conditions on your solution * - It should return a solution if there * exists one. * - If no solution can be found, return the * original input array * - If multiple solutions exist, any one of * them is valid * * - Can I use helper functions? * - Yes, by all means! * * - Can I add extra libraries? * - No, they won't be needed. * * - Can I change the 'print_sudoku()' function? * - Please don't, we may need it for testing */ #include<stdio.h> #include<stdlib.h> #include<string.h> void print_sudoku(int sudoku[9][9]) { printf("The Sudoku contains:\n"); for (int j=0; j<9; j++) { for (int i=0; i<9;i++) { printf("%d ",sudoku[j][i]); } printf("\n"); } } int check_sudoku(int sudoku[9][9]) { /* * This function checks whether a Sudoku is properly * solved. That means that each row, column, and * 3x3 subgrid uses each digit only once. * * If *ignores* zeros, so you can use it to check * that a partial solution is valid */ /***** * TO DO: * Complete this function * -- BUT ONLY IF YOU WANT TO! * Our testing script only checks your Sudoku * solution, if you want to solve it without * using this function, or if you want to write * your own helper functions, that's ok! * *****/ return 0; } int solved(int sudoku[9][9]) { /* * This function checks whether a given Sudoku is * completely solved (it has no zeros, all digits * are used once per row, column, and sub-grid. * It returns 1 if the Sudoku is solved, zero * otherwise */ /***** * TO DO * Complete this function * BUT ONLY IF YOU WANT TO! * You can solve the Sudoku without using this function, * or you can write a different helper function, whatever * makes more sense to yoU! our checker won't look at this * function, * ****/ return 0; } void solve_sudoku(int sudoku[9][9], int depth) { /* * This function solves an input Sudoku, the * input has a value of 0 for any entries that * are not yet decided. If no solution exists * the function returns the input array *exactly * as it was* when the function was called. * * The 'depth' parameter is provided in case you * want to use it to help debug and trace your * code. You do not need to use it if you don't * want to, and the problem can be solved without * using the depth value at all. * * The automated checker won't look at the depth * either. */ /***** * TO DO: * Complete this function *****/ } #ifndef __testing int main() { // We'll provide you with the same test case // from the Wiki, so you can check your output. // But as always, be aware we will test your // solution on different Sudoku puzzles. int Sudoku[9][9]={{5, 3, 0, 0, 7, 0, 0, 0, 0}, {6, 0, 0, 1, 9, 5, 0, 0, 0}, {0, 9, 8, 0, 0, 0, 0, 6, 0}, {8, 0, 0, 0, 6, 0, 0, 0, 3}, {4, 0, 0, 8, 0, 3, 0, 0, 1}, {7, 0, 0, 0, 2, 0, 0, 0, 6}, {0, 6, 0, 0, 0, 0, 2, 8, 0}, {0, 0, 0, 4, 1, 9, 0, 0, 5}, {0, 0, 0, 0, 8, 0, 0, 7, 9}}; printf("Input puzzle is:\n"); print_sudoku(Sudoku); solve_sudoku(Sudoku,0); printf("Solution is:\n"); print_sudoku(Sudoku); } #endif
Please find the answer below:
I have completed the function to solve the sudoku. I have mentioned the details in the comments
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void print_sudoku(int sudoku[9][9]);
int check_sudoku(int sudoku[9][9]);
int solved(int sudoku[9][9]);
int is_exist_row(int sudoku[9][9], int row, int num);
int is_exist_col(int sudoku[9][9], int col, int num);
int is_exist_box(int sudoku[9][9], int startRow, int startCol, int
num);
int is_safe_num(int sudoku[9][9], int row, int col, int num);
int find_next_location(int sudoku[9][9], int *row, int *col);
int solve_sudoku_helper(int sudoku[9][9]);
void print_sudoku(int sudoku[9][9])
{
printf("The Sudoku contains:\n");
for (int j=0; j<9; j++)
{
for (int i=0; i<9;i++)
{
printf("%d ",sudoku[j][i]);
}
printf("\n");
}
}
int check_sudoku(int sudoku[9][9])
{
/*
* This function checks whether a Sudoku is properly
* solved. That means that each row, column, and
* 3x3 subgrid uses each digit only once.
*
* If *ignores* zeros, so you can use it to check
* that a partial solution is valid
*/
/*****
* TO DO:
* Complete this function
* -- BUT ONLY IF YOU WANT TO!
* Our testing script only checks your Sudoku
* solution, if you want to solve it without
* using this function, or if you want to write
* your own helper functions, that's ok!
* *****/
return 0;
}
int solved(int sudoku[9][9])
{
/*
* This function checks whether a given Sudoku is
* completely solved (it has no zeros, all digits
* are used once per row, column, and sub-grid.
* It returns 1 if the Sudoku is solved, zero
* otherwise
*/
/*****
* TO DO
* Complete this function
* BUT ONLY IF YOU WANT TO!
* You can solve the Sudoku without using this function,
* or you can write a different helper function, whatever
* makes more sense to yoU! our checker won't look at this
* function,
* ****/
}
//check uniqueness in row
int is_exist_row(int sudoku[9][9], int row, int num){
for (int col = 0; col < 9; col++) {
if (sudoku[row][col] == num)
{
return 1;
}
}
return 0;
}
//check the uniqueness in column
int is_exist_col(int sudoku[9][9], int col, int num) {
for (int row = 0; row < 9; row++) {
if (sudoku[row][col] == num)
{
return 1;
}
}
return 0;
}
//check uniqueness in the 3*3 grid box
int is_exist_box(int sudoku[9][9], int startRow, int startCol, int
num) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3;
col++) {
if (sudoku[row +
startRow][col + startCol] == num) {
return 1;
}
}
}
return 0;
}
//check if the putting th number at this location is safe or
not
int is_safe_num(int sudoku[9][9], int row, int col, int num)
{
return !is_exist_row(sudoku, row, num)
&&
!is_exist_col(sudoku, col, num)
&&
!is_exist_box(sudoku, row - (row % 3), col - (col %3), num);
}
//get the next location which has 0
int find_next_location(int sudoku[9][9], int *row, int *col)
{
for (*row = 0; *row < 9; (*row)++) {
for (*col = 0; *col < 9;
(*col)++) {
if
(sudoku[*row][*col] == 0) {
return 1;
}
}
}
return 0;
}
//sudoku solver function
int solve_sudoku_helper(int sudoku[9][9]){
//variable declaration
int row = 0;
int col = 0;
//get the next location which contain 0 in the array
//if all the locations are filled return success
if (!find_next_location(sudoku, &row, &col)){
return 1;
}
//check by putting all the number in the location and
recursively call the function for the next location
for (int num = 1; num <= 9; num++ ) {
if (is_safe_num(sudoku, row, col,
num)) {
sudoku[row][col]
= num;
//if current location can contain value then store it
if
(solve_sudoku_helper(sudoku)) {
return 1;
}
//else set the location to 0 again and return
sudoku[row][col]
= 0;
}
}
//return if the solution doesn't exist.
return 0;
}
void solve_sudoku(int Sudoku[9][9], int depth){
/*
* This function solves an input Sudoku, the
* input has a value of 0 for any entries that
* are not yet decided. If no solution exists
* the function returns the input array *exactly
* as it was* when the function was called.
*
* The 'depth' parameter is provided in case you
* want to use it to help debug and trace your
* code. You do not need to use it if you don't
* want to, and the problem can be solved without
* using the depth value at all.
*
* The automated checker won't look at the depth
* either.
*/
/*****
* TO DO:
* Complete this function
*****/
//create copy of original sudoku to take care of no solution
case and return original array
int original[9][9];
memcpy(original,Sudoku,sizeof original);
//if solution doesn't exist then return original array
if (!solve_sudoku_helper(Sudoku)){
memcpy(Sudoku,original, sizeof
original);
}
return;
}
#ifndef __testing
int main(){
// We'll provide you with the same test case
// from the Wiki, so you can check your output.
// But as always, be aware we will test your
// solution on different Sudoku puzzles.
int Sudoku[9][9]={{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
printf("Input puzzle is:\n");
print_sudoku(Sudoku);
solve_sudoku(Sudoku,0);
printf("Solution is:\n");
print_sudoku(Sudoku);
}
#endif
Output:
* Your goal in this exercise is to practice recursion and * to see how a...
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...
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,...
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...
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....
Must be written in C 89 Mode Array Insertion Your task is to complete the implementation of the insertion function, insert_into_array: int* insert_into_array(int arr[], size_t arr_len, int value, size_t pos); The insertion function inserts a value into an array at a specified position. All elements to the right of the inserted element are shifted over a position (upon inserting, all elements at the insertion position are shifted up an index, and the last element in the array is overwritten by...
This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!! Modify my code if anything return statement seems misplaced or inaccurately called. Here is my code: int write_sudoku_board(const char file_name[ ], int board[9][9]) { int number; int i,j; int a = 9; FILE* fp = fopen("sudoku.txt", "w"); int count = 0; for(i =...
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...
4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized to functions, allowing easy reuse. Recall that arrays in C are essentially represented by pointers, so when an array is passed into a function, the function is given access to the original array data (not a copy). This means that arrays are effectively passed by reference in C, and therefore that functions must be careful not to modify the contents of arrays they receive...
How do I set up my read function? here's my code so far: int read_sudoku_board(const char file_name[], int board[9][9]) { FILE * fp = fopen("sudoku.txt", "r"); int a,i,j,c; int count = 0; for(i = 0; i < a; i++){ for(j = 0;j < 9;j++){ c = fgetc(fp); if(c == '-'){ board[i][j] = 0; } else if(c >= 1 && c <= 9) printf(" %d"); else return -2; } count++; } if(count != a-1) return -1; else return 0; }12 Read...
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 //...