

# function to check if the value to be assigned to a cell already exists in that column of that cell # it returns true if 'val' can be placed in a cell with row number as 'row' # function to check if the value to be assigned to a cell already exists in that row of that cell If the cell (i,j) can't be filled with num the mark it as unfilled to trigger backtrackingĤ.If none of the numbers from 1 to 9 can be filled in cell (i,j) then return false as there is no solution for this sudoku Implementation # function to print grid If sudokuSolver( grid) is true then return trueģ.3. If the cell (i,j) can be filled with num then fill it with num temporarily to checkģ.2. A valid sudoku is obtained hence return trueģ.1. If( solve(grid with choice c made) is true)Ģ.1. Hence the center cell for box containing (5,6) is (4,7)Īfter finding the center cell it easy to check as all the values in the box will be at unit distance from the center cell. To check the 3×3 submatrix(box) condition If at any point none of the numbers can be placed at (i,j) then we've to backtrack and change the values for already visited cells If all the constraints are true then we'll place that number at (i,j) and then repeat the process by finding an unfilled cell. If any of the any of the constraint becomes false we'll not place that number at (i,j). Before placing we check for the constraints by checking the current row,current column and current 3×3 submatrix. We try to place every number between 1 to 9 in the unfilled cell. If all the cells are filled then a valid sudoku is obtained. We start by finding an unfilled cell(i,j). " This algorithm visits all the empty cells in some order,filling the digits incrementally, backtracks when a number is not found to be valid.The program starts by filling '1' in the first cell if there are no violations, then advances to the second cell and places '1' incase violation occurs then increments and places '2'.It checks incrementally in this way and when none of the '1' to '9' numbers fit in this cell then the algorithm leaves the cell blank and goes back to the previous cell.The value in that cell is incremented and this process continues until all the cells are filled." STEPS:

The difference between brute-force and backtracking is that in backtracking, we do not explore all the possible options instead we explore only worthy options and build the solution incrementally. Sudoku can be solved in the way mentioned above. 3)Backtrackingīacktracking is a kind of brute-force approach which comes into picture when solving a problem requires considering multiple choices as we don't know which choice is correct and we try to solve the problem using trail and error method considering one choice at a time until required answer is obtained. A slight optimization to this would be the backtracking approach. This can be much time consuming and highly inefficient. In naive approach,the algorithm fills in the empty cells without any logic and later checks whether it was a valid placement. Given a partially filled sudoku(Fig 1.a) and a solution to it(Fig 1.b)Ī sudoku is valid if it satisfies all the following properties:ġ.Each row contains unique values ranging from 1 to 9.Ģ.Each column contains unique values ranging from 1 to 9.ģ.Each of 9 sub-squares ,of size 3×3, contains unique values from 1 to 9. Given a 9×9 grid, with some initial values filled.The objective is to fill all the empty cells of the grid with numbers such that it becomes valid. Sudoku is a logic based number placement puzzle. Probably you might know what sudoku is or might have heard somewhere about it.Let's begin with a brief note on it. We have presented the Time and Space Complexity for various cases. In this article, we have covered the Backtracking Algorithm for Sudoku and compared with the Brute Force approach.
