Last active
December 26, 2017 17:22
-
-
Save GeorgeSeif/54a78dcabe2c1effb4969d82a88be6c9 to your computer and use it in GitHub Desktop.
Defines the rules of Sudoku
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Returns a boolean which indicates whether any assigned entry | |
| // in the specified row matches the given number. | |
| bool used_in_row(int grid[DIM][DIM], int row, int num) | |
| { | |
| for (int col = 0; col < DIM; col++) | |
| if (grid[row][col] == num) | |
| { | |
| return true; | |
| } | |
| return false; | |
| } | |
| // Returns a boolean which indicates whether any assigned entry | |
| // in the specified column matches the given number. | |
| bool used_in_col(int grid[DIM][DIM], int col, int num) | |
| { | |
| for (int row = 0; row < DIM; row++) | |
| if (grid[row][col] == num) | |
| { | |
| return true; | |
| } | |
| return false; | |
| } | |
| // Returns a boolean which indicates whether any assigned entry | |
| // within the specified 3x3 box matches the given number. | |
| bool used_in_box(int grid[DIM][DIM], int box_start_rpw, int box_start_col, int num) | |
| { | |
| for (int row = 0; row < 3; row++) | |
| for (int col = 0; col < 3; col++) | |
| if (grid[row + box_start_rpw][col + box_start_col] == num) | |
| { | |
| return true; | |
| } | |
| return false; | |
| } | |
| // Returns a boolean which indicates whether it will be legal to assign | |
| // num to the given row,col location. | |
| bool is_safe(int grid[DIM][DIM], int row, int col, int num) | |
| { | |
| // Check if 'num' is not already placed in current row, | |
| // current column and current 3x3 box | |
| return !used_in_row(grid, row, num) && | |
| !used_in_col(grid, col, num) && | |
| !used_in_box(grid, row - row % 3, col - col % 3, num); | |
| } | |
| // Searches the grid to find an entry that is still unassigned. If | |
| // found, the reference parameters row, col will be set the location | |
| // that is unassigned, and true is returned. If no unassigned entries | |
| // remain, false is returned. | |
| std::pair<int, int> get_unassigned_location(int grid[DIM][DIM]) | |
| { | |
| for (int row = 0; row < DIM; row++) | |
| for (int col = 0; col < DIM; col++) | |
| if (grid[row][col] == BLANK) | |
| { | |
| return std::make_pair(row, col); | |
| } | |
| return GRID_FULL; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment