Sudoku Solver designed to solve Sudoku puzzles using Backtracking Algorithm. Sudoku is a popular logic-based number placement puzzle game that involves a 9x9 grid divided into nine 3x3 subgrids or "regions." The goal of Sudoku is to fill in the grid with numbers from 1 to 9 in such a way that each row, each column, and each of the nine 3x3 regions contains all of the digits from 1 to 9 without any repetition.
Two options to select for input:
- manually input the puzzle.
- generate the puzzle.
- Fill all the diagonal 3x3 matrices.
- Fill recursively the rest of the non-diagonal matrices. For every cell to be filled, we try all numbers until we find a safe number to be placed.
- Once the matrix is fully filled, remove K elements randomly to complete game
Sudoku Solver using Backtracking Algorithm:
- Create a function that checks after assigning the current index the grid becomes unsafe or not. Keep Hashmap for a row, column and boxes. If any number has a frequency greater than 1 in the hashMap return false else return true; hashMap can be avoided by using loops.
- Create a recursive function that takes a grid.
- Check for any unassigned location.
- If present then assigns a number from 1 to 9.
- Check if assigning the number to the current index makes the grid unsafe or not.
- If safe then recursively call the function for all safe cases from 0 to 9.
- If any recursive call returns true, end the loop and return true. If no recursive call returns true then return false.
- If there is no unassigned location then return true.