Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code: #include #include // Declare printSudoku function void printSudoku( int ***); // Declare solveSudoku function int solveSudoku( int ***); //Helper functions. You can define any

image text in transcribed

image text in transcribed

image text in transcribed

Code:

#include

#include

// Declare printSudoku function

void printSudoku(int***);

// Declare solveSudoku function

int solveSudoku(int***);

//Helper functions. You can define any functions that can help your solve the problem

/*

The main program reads a text file containing the block values of the Sudoku grid.

It then saves each 3x3 block into a 2D array. The Sudoku grid is composed of 9 3x3 blocks.

DO NOT MODIFY THE MAIN FUNTION!!!

*/

int main(int argc, char **argv) {

if (argc != 2) {

fprintf(stderr, "Usage: %s ", argv[0]);

return 2;

}

int i, j;

FILE *fptr;

int ***blocks = (int***)malloc(9 * sizeof(int**));

// Open file for reading

fptr = fopen(argv[1], "r");

if (fptr == NULL) {

printf("Cannot Open File! ");

return 0;

}

// Read 9x9 blocks into 2D arrays

for(i=0; i

{

*(blocks+i) = (int**)malloc(3 * sizeof(int*));

printf("Reading numbers in block %d... ", i+1);

for(j=0; j

{

*(*(blocks+i)+j) = (int*)malloc(3 * sizeof(int));

fscanf(fptr, "%d %d %d", *(*(blocks+i)+j), *(*(blocks+i)+j)+1, *(*(blocks+i)+j)+2);

printf("%d %d %d ", *(*(*(blocks+i)+j)), *(*(*(blocks+i)+j)+1), *(*(*(blocks+i)+j)+2));

}

}

// Print out original Sudoku grid

printf("Printing Sudoku before being solved: ");

printSudoku(blocks);

// Call solveSudoku and print out result

printf(" Solving Sudoku... ");

if(solveSudoku(blocks)){

printf("Sudoku solved! ");

printSudoku(blocks);

}

else printf("This Sudoku cannot be solved! ");

return 0;

}

void printSudoku(int*** arr){

// This function will print out the complete Sudoku grid (arr). It must produce the output in the SAME format as the samples in the instructions.

// Your implementation here

}

int solveSudoku(int*** blocks){

// This is the function to solve the Sudoku (blocks). Feel free to use any helper functions.

// YOU MUST NOT USE ANY ARRAY NOTATION ([])!

//Your implementation here

return 0;

}

Sudoku Sudoku is a number placement puzzle. If you do not know about this puzzle, please visit: https://en.wikipedia.org/wiki/Sudoku to find out more about how this puzzle works. Instead of having a 9x9 Sudoku grid as a 2D array, our version of Sudoku grid is composed of 9 3x3 blocks, where each block is stored in a 2D array using int** (in Lab #4 you have learned how to use int** to construct a 2D array). As a result, a triple pointer (int***) is used to arrange these 9 3x3 blocks at the following order: int** int** int** int** int** int** int** int** int** int intint intintintin int intinint int int int intint intintintin int int int intint nt ntint intn int intint nint intint intint int int intint int* Figure 1. Organization of Sudoku grid Your tasks Your tasks is to implement 2 functions to complete the program (sudoku3d.c): printSudoku(int***) This function will take in the Sudoku grid and print out the comtent. Since this grid is form by 9 3x3 blocks, you need to be careful about their order and orientations. Implement this function so that it will print out the content in the SAME format as the sample output. solveSudoku(int***) This is function is the core of solving the puzzle. It till take in the Sudoku grid and solve it. In this project, we will use a brute-force witlh backtracking approach to solve the puzzle. In another word, we will keep filling the O's in the grid with all possible values until the puzzle is solved. If a value does not work, it will take a step back and try with another value; therefore, you must implement this as a recursive function. You can find more about the backtracking approach here: https://en.wikipedia.org/wiki/Sudoku_solving_algorithms Feel free to declare any helper functions in this program to keep your program organized. YOU MUST NOT USE ANY ARRAY NOTATION ([]) in this program! Testing your program After compiling sudoku3d.c, run the program by typing: /sudoku blocks1.txt Where sudoku is the executable file, and blocks1.txt is the text file containing the values of each 3x3 blocks. Feel free to create your own text files for test cases. The order of the blocks is according to Figure 1. There are many puzzles and answers available online. EBoxlBox SyncBox SyncluC MercediTeachingiCSE3TICSE31 S19VProje Reading numbers in block 1.. . 1 9 0 0 5 2 Reading numbers in block 2... 4 7 0 8 1 9 Reading numbers in block 3 0 9 0 6 0 8 4 0 7 Reading numbers in block 4. .. 2 0 0 0 0 9 Reading numbers in block 5... 0 4 8 7 5 0 Reading numbers in block 6 5 0 0 0 0 9 Reading numbers in block 7... 9 0 7 5 0 6 0 8 0 Reading numbers in block 8... 3 6 4 0 8 1 Reading numbers in block 9 1 80 0 74 Printing Sudoku before being solved: 0001000 10 90I 1901470 1608 0 5 21819 1407 200 10 48 100 0 I 009100 0 15 0 0I 0001750 10 09 90 71364 1180 I 5 06 1081 10 74 I 0 8 0 1000 10 00 Solving Sudoku.. - Sudoku solved! 7 481635 1291 I 1 9 3 147 2 165 8 I 6 5 2 18 1914 37I 2651948 1713 I 8 7911 23 15 46 I 3 1 4175618 29 I 9 271364 11 8 5 I 5 3 6 1281 19 74 I 481 1597 136 2 I Sudoku Sudoku is a number placement puzzle. If you do not know about this puzzle, please visit: https://en.wikipedia.org/wiki/Sudoku to find out more about how this puzzle works. Instead of having a 9x9 Sudoku grid as a 2D array, our version of Sudoku grid is composed of 9 3x3 blocks, where each block is stored in a 2D array using int** (in Lab #4 you have learned how to use int** to construct a 2D array). As a result, a triple pointer (int***) is used to arrange these 9 3x3 blocks at the following order: int** int** int** int** int** int** int** int** int** int intint intintintin int intinint int int int intint intintintin int int int intint nt ntint intn int intint nint intint intint int int intint int* Figure 1. Organization of Sudoku grid Your tasks Your tasks is to implement 2 functions to complete the program (sudoku3d.c): printSudoku(int***) This function will take in the Sudoku grid and print out the comtent. Since this grid is form by 9 3x3 blocks, you need to be careful about their order and orientations. Implement this function so that it will print out the content in the SAME format as the sample output. solveSudoku(int***) This is function is the core of solving the puzzle. It till take in the Sudoku grid and solve it. In this project, we will use a brute-force witlh backtracking approach to solve the puzzle. In another word, we will keep filling the O's in the grid with all possible values until the puzzle is solved. If a value does not work, it will take a step back and try with another value; therefore, you must implement this as a recursive function. You can find more about the backtracking approach here: https://en.wikipedia.org/wiki/Sudoku_solving_algorithms Feel free to declare any helper functions in this program to keep your program organized. YOU MUST NOT USE ANY ARRAY NOTATION ([]) in this program! Testing your program After compiling sudoku3d.c, run the program by typing: /sudoku blocks1.txt Where sudoku is the executable file, and blocks1.txt is the text file containing the values of each 3x3 blocks. Feel free to create your own text files for test cases. The order of the blocks is according to Figure 1. There are many puzzles and answers available online. EBoxlBox SyncBox SyncluC MercediTeachingiCSE3TICSE31 S19VProje Reading numbers in block 1.. . 1 9 0 0 5 2 Reading numbers in block 2... 4 7 0 8 1 9 Reading numbers in block 3 0 9 0 6 0 8 4 0 7 Reading numbers in block 4. .. 2 0 0 0 0 9 Reading numbers in block 5... 0 4 8 7 5 0 Reading numbers in block 6 5 0 0 0 0 9 Reading numbers in block 7... 9 0 7 5 0 6 0 8 0 Reading numbers in block 8... 3 6 4 0 8 1 Reading numbers in block 9 1 80 0 74 Printing Sudoku before being solved: 0001000 10 90I 1901470 1608 0 5 21819 1407 200 10 48 100 0 I 009100 0 15 0 0I 0001750 10 09 90 71364 1180 I 5 06 1081 10 74 I 0 8 0 1000 10 00 Solving Sudoku.. - Sudoku solved! 7 481635 1291 I 1 9 3 147 2 165 8 I 6 5 2 18 1914 37I 2651948 1713 I 8 7911 23 15 46 I 3 1 4175618 29 I 9 271364 11 8 5 I 5 3 6 1281 19 74 I 481 1597 136 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Genomes And Databases On The Internet A Practical Guide To Functions And Applications

Authors: Paul Rangel

1st Edition

189848631X, 978-1898486312

More Books

Students also viewed these Databases questions