} Driver Code Ends
| 11 | |
| 12 | // } Driver Code Ends |
| 13 | class Solution |
| 14 | { |
| 15 | public: |
| 16 | //Function to find a solved Sudoku. |
| 17 | bool check(int grid[N][N],int row, int col, int num){ |
| 18 | //row |
| 19 | for(int i=0;i<9;i++){ |
| 20 | if(grid[i][col]==num) return false; |
| 21 | } |
| 22 | //col |
| 23 | for(int j=0;j<9;j++){ |
| 24 | if(grid[row][j]==num) return false; |
| 25 | } |
| 26 | //box |
| 27 | int ini =row-row%3; |
| 28 | int inj=col-col%3; |
| 29 | |
| 30 | for(int i=0;i<3;i++){ |
| 31 | for(int j=0;j<3;j++){ |
| 32 | if(grid[ini+i][inj+j]==num) return false; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return true; |
| 37 | } |
| 38 | bool helper(int grid[N][N], int row ,int col){ |
| 39 | if (row==9) return true; |
| 40 | if (col==9) return helper(grid,row+1,0); |
| 41 | if (grid[row][col] != 0) return helper(grid,row,col+1); |
| 42 | |
| 43 | for(int i=1;i<=9;i++){ |
| 44 | if (check(grid,row,col,i)){ |
| 45 | grid[row][col]=i; |
| 46 | if (helper(grid,row,col+1)){ |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | grid[row][col]=0; |
| 51 | } |
| 52 | return false; |
| 53 | |
| 54 | } |
| 55 | bool SolveSudoku(int grid[N][N]) |
| 56 | { |
| 57 | // Your code here |
| 58 | return helper(grid,0,0); |
| 59 | } |
| 60 | |
| 61 | //Function to print grids of the Sudoku. |
| 62 | void printGrid (int grid[N][N]) |
| 63 | { |
| 64 | // Your code here |
| 65 | |
| 66 | for(int i=0;i<9;i++){ |
| 67 | for(int j=0;j<9;j++){ |
| 68 | cout<<grid[i][j]<<" "; |
| 69 | } |
| 70 | } |
nothing calls this directly
no outgoing calls
no test coverage detected