Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "sudoku.h"
- //-------------------------------------------------------------------------------------------------
- // Start here to work on your MP7
- //-------------------------------------------------------------------------------------------------
- // You are free to declare any private functions if needed.
- // Function: is_val_in_row
- // Return true if "val" already existed in ith row of array sudoku.
- int is_val_in_row(const int val, const int i, const int sudoku[9][9]) {
- assert(i>=0 && i<9);
- // BEG TODO
- assert(i>=0 && i<9);
- // BEG TODO
- int a=0;
- int count=0;
- for(a=0; a<9;a++){
- if(sudoku[i][a]==val)
- count++;
- }
- if (count>0)
- return 1;
- return 0;
- // END TODO
- }
- // Function: is_val_in_col
- // Return true if "val" already existed in jth column of array sudoku.
- int is_val_in_col(const int val, const int j, const int sudoku[9][9]) {
- assert(j>=0 && j<9);
- // BEG TODO
- int a=0;
- int count=0;
- for(a=0; a<9;a++){
- if(sudoku[a][j]==val)
- count++;
- }
- if (count>0)
- return 1;
- return 0;
- // END TODO
- }
- // Function: is_val_in_3x3_zone
- // Return true if val already existed in the 3x3 zone corresponding to (i, j)
- int is_val_in_3x3_zone(const int val, const int i, const int j, const int sudoku[9][9]) {
- assert(i>=0 && i<9);
- // BEG TODO
- int rw=i-i%3;
- int cl=j-j%3;
- int count=0;
- int row=rw*3;
- int col=cl*3;
- int a;
- int b;
- for(a=0;a<3;a++){
- for(b=0;b<3;b++){
- if(val==sudoku[a+rw][b+cl])
- count++;
- }
- }
- if (count>0)
- return 1;
- return 0;
- // END TODO
- }
- // Function: is_val_valid
- // Return true if the val is can be filled in the given entry.
- int is_val_valid(const int val, const int i, const int j, const int sudoku[9][9]) {
- assert(i>=0 && i<9 && j>=0 && j<9);
- // BEG TODO
- if(is_val_in_row(val, i, sudoku)==0&&is_val_in_col(val, j, sudoku)==0&&is_val_in_3x3_zone(val, i, j,sudoku)==0){
- return 1;
- }
- else
- return 0;
- // END TODO
- }
- // Procedure: solve_sudoku
- // Solve the given sudoku instance.
- int solve_sudoku(int sudoku[9][9]) {
- // BEG TODO.
- /* int i, j;
- if (all cells are assigned by number) {
- return true;
- }
- else {
- find a non-filled cell (i, j)
- }
- for (int num = 1; num <= 9; num++) {
- if (cell (i, j) can be filled with num) {
- sudoku[i][j] <- num;
- if (solve_sudoku(sudoku)) {
- return true;
- }
- sudoku[i][j] <- non-filled;
- }
- }
- return false;
- return 0;
- // END TODO.
- */
- }
- // Procedure: print_sudoku
- void print_sudoku(int sudoku[9][9])
- {
- int i, j;
- for(i=0; i<9; i++) {
- for(j=0; j<9; j++) {
- printf("%2d", sudoku[i][j]);
- }
- printf("\n");
- }
- }
- // Procedure: parse_sudoku
- void parse_sudoku(const char fpath[], int sudoku[9][9]) {
- FILE *reader = fopen(fpath, "r");
- assert(reader != NULL);
- int i, j;
- for(i=0; i<9; i++) {
- for(j=0; j<9; j++) {
- fscanf(reader, "%d", &sudoku[i][j]);
- }
- }
- fclose(reader);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement