|
| 1 | +// A Backtracking program in C++ to solve Sudoku problem |
| 2 | +#include <stdio.h> |
| 3 | + |
| 4 | +// UNASSIGNED is used for empty cells in sudoku grid |
| 5 | +#define UNASSIGNED 0 |
| 6 | + |
| 7 | +// N is used for size of Sudoku grid. Size will be NxN |
| 8 | +#define N 9 |
| 9 | + |
| 10 | +// This function finds an entry in grid that is still unassigned |
| 11 | +bool FindUnassignedLocation(int grid[N][N], int &row, int &col); |
| 12 | + |
| 13 | +// Checks whether it will be legal to assign num to the given row,col |
| 14 | +bool isSafe(int grid[N][N], int row, int col, int num); |
| 15 | + |
| 16 | +/* Takes a partially filled-in grid and attempts to assign values to |
| 17 | + all unassigned locations in such a way to meet the requirements |
| 18 | + for Sudoku solution (non-duplication across rows, columns, and boxes) */ |
| 19 | +bool SolveSudoku(int grid[N][N]) |
| 20 | +{ |
| 21 | + int row, col; |
| 22 | + |
| 23 | + // If there is no unassigned location, we are done |
| 24 | + if (!FindUnassignedLocation(grid, row, col)) |
| 25 | + return true; // success! |
| 26 | + |
| 27 | + // consider digits 1 to 9 |
| 28 | + for (int num = 1; num <= 9; num++) |
| 29 | + { |
| 30 | + // if looks promising |
| 31 | + if (isSafe(grid, row, col, num)) |
| 32 | + { |
| 33 | + // make tentative assignment |
| 34 | + grid[row][col] = num; |
| 35 | + |
| 36 | + // return, if success, yay! |
| 37 | + if (SolveSudoku(grid)) |
| 38 | + return true; |
| 39 | + |
| 40 | + // failure, unmake & try again |
| 41 | + grid[row][col] = UNASSIGNED; |
| 42 | + } |
| 43 | + } |
| 44 | + return false; // this triggers backtracking |
| 45 | +} |
| 46 | + |
| 47 | +/* Searches the grid to find an entry that is still unassigned. If |
| 48 | + found, the reference parameters row, col will be set the location |
| 49 | + that is unassigned, and true is returned. If no unassigned entries |
| 50 | + remain, false is returned. */ |
| 51 | +bool FindUnassignedLocation(int grid[N][N], int &row, int &col) |
| 52 | +{ |
| 53 | + for (row = 0; row < N; row++) |
| 54 | + for (col = 0; col < N; col++) |
| 55 | + if (grid[row][col] == UNASSIGNED) |
| 56 | + return true; |
| 57 | + return false; |
| 58 | +} |
| 59 | + |
| 60 | +/* Returns a boolean which indicates whether any assigned entry |
| 61 | + in the specified row matches the given number. */ |
| 62 | +bool UsedInRow(int grid[N][N], int row, int num) |
| 63 | +{ |
| 64 | + for (int col = 0; col < N; col++) |
| 65 | + if (grid[row][col] == num) |
| 66 | + return true; |
| 67 | + return false; |
| 68 | +} |
| 69 | + |
| 70 | +/* Returns a boolean which indicates whether any assigned entry |
| 71 | + in the specified column matches the given number. */ |
| 72 | +bool UsedInCol(int grid[N][N], int col, int num) |
| 73 | +{ |
| 74 | + for (int row = 0; row < N; row++) |
| 75 | + if (grid[row][col] == num) |
| 76 | + return true; |
| 77 | + return false; |
| 78 | +} |
| 79 | + |
| 80 | +/* Returns a boolean which indicates whether any assigned entry |
| 81 | + within the specified 3x3 box matches the given number. */ |
| 82 | +bool UsedInBox(int grid[N][N], int boxStartRow, int boxStartCol, int num) |
| 83 | +{ |
| 84 | + for (int row = 0; row < 3; row++) |
| 85 | + for (int col = 0; col < 3; col++) |
| 86 | + if (grid[row+boxStartRow][col+boxStartCol] == num) |
| 87 | + return true; |
| 88 | + return false; |
| 89 | +} |
| 90 | + |
| 91 | +/* Returns a boolean which indicates whether it will be legal to assign |
| 92 | + num to the given row,col location. */ |
| 93 | +bool isSafe(int grid[N][N], int row, int col, int num) |
| 94 | +{ |
| 95 | + /* Check if 'num' is not already placed in current row, |
| 96 | + current column and current 3x3 box */ |
| 97 | + return !UsedInRow(grid, row, num) && |
| 98 | + !UsedInCol(grid, col, num) && |
| 99 | + !UsedInBox(grid, row - row%3 , col - col%3, num); |
| 100 | +} |
| 101 | + |
| 102 | +/* A utility function to print grid */ |
| 103 | +void printGrid(int grid[N][N]) |
| 104 | +{ |
| 105 | + for (int row = 0; row < N; row++) |
| 106 | + { |
| 107 | + for (int col = 0; col < N; col++) |
| 108 | + printf("%2d", grid[row][col]); |
| 109 | + printf("\n"); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/* Driver Program to test above functions */ |
| 114 | +int main() |
| 115 | +{ |
| 116 | + // 0 means unassigned cells |
| 117 | + int grid[N][N] = {{3, 0, 6, 5, 0, 8, 4, 0, 0}, |
| 118 | + {5, 2, 0, 0, 0, 0, 0, 0, 0}, |
| 119 | + {0, 8, 7, 0, 0, 0, 0, 3, 1}, |
| 120 | + {0, 0, 3, 0, 1, 0, 0, 8, 0}, |
| 121 | + {9, 0, 0, 8, 6, 3, 0, 0, 5}, |
| 122 | + {0, 5, 0, 0, 9, 0, 6, 0, 0}, |
| 123 | + {1, 3, 0, 0, 0, 0, 2, 5, 0}, |
| 124 | + {0, 0, 0, 0, 0, 0, 0, 7, 4}, |
| 125 | + {0, 0, 5, 2, 0, 6, 3, 0, 0}}; |
| 126 | + if (SolveSudoku(grid) == true) |
| 127 | + printGrid(grid); |
| 128 | + else |
| 129 | + printf("No solution exists"); |
| 130 | + |
| 131 | + return 0; |
| 132 | +} |
0 commit comments