diff --git a/Sudoku-Solver/README.md b/Sudoku-Solver/README.md new file mode 100644 index 000000000..b9eb5883e --- /dev/null +++ b/Sudoku-Solver/README.md @@ -0,0 +1,31 @@ +# Sudoku-Solver + + If you want a solution for a sudoku puzzle, it may help you. Give each line of the puzzle as input with zeroes in place of spaces. + + This program uses backtracking process to solve. The program finds the zeroes i.e. empty spaces in the line and checks the number in range 9 that it unique in that row , column and square. Like that the code goes on checking each and every space in the puzzle. If any number is repeated inside the row and column, then it is backtrack and changes the number in the respective space. The program uses the recursion to backtrack in the puzzle. + + # Input + 7 8 0 4 0 0 1 2 0 + 6 0 0 0 7 5 0 0 9 + 0 0 0 6 0 1 0 7 8 + 0 0 7 0 4 0 2 6 0 + 0 0 1 0 5 0 9 3 0 + 9 0 4 0 6 0 0 0 5 + 0 7 0 3 0 0 0 1 2 + 1 2 0 0 0 7 4 0 0 + 0 4 9 2 0 6 0 0 7 + + # Output + 7 8 5 | 4 3 9 | 1 2 6 + 6 1 2 | 8 7 5 | 3 4 9 + 4 9 3 | 6 2 1 | 5 7 8 + - - - - - - - - - - - - - + 8 5 7 | 9 4 3 | 2 6 1 + 2 6 1 | 7 5 8 | 9 3 4 + 9 3 4 | 1 6 2 | 7 8 5 + - - - - - - - - - - - - - + 5 7 8 | 3 9 4 | 6 1 2 + 1 2 6 | 5 8 7 | 4 9 3 + 3 4 9 | 2 1 6 | 8 5 7 + Check for more solution? + Thank you diff --git a/Sudoku-Solver/solve_sudoku.py b/Sudoku-Solver/solve_sudoku.py new file mode 100644 index 000000000..0a60c9e6b --- /dev/null +++ b/Sudoku-Solver/solve_sudoku.py @@ -0,0 +1,68 @@ +def is_number_valid(row, col, num): + # function to find if the number is valid or not in the respective space + global grid # making grid as global variable + for i in range(0, 9): + # for loop to find if the number is present in the row or not + if grid[row][i] == num: + return False + for i in range(0, 9): + # for loop to find if the number is present in the column or not + if grid[i][col] == num: + return False + # makes the variables to consider the 3x3 square in the puzzle + col_subset = (col // 3) * 3 + row_subset = (row // 3) * 3 + for i in range(0, 3): + # for loop to find if the number is present in square + for j in range(0, 3): + if grid[row_subset + i][col_subset + j] == num: + return False + return True # if not preent in the all the possibilities returns True + + +def print_board(): # function to print the puzzle after solving + global grid # global variables + for i in range(len(grid)): # for loop to print the board + if i % 3 == 0 and i != 0: + print("- - - - - - - - - - - - - ") + + for j in range(len(grid[0])): + if j % 3 == 0 and j != 0: + print(" | ", end="") + + if j == 8: + print(grid[i][j]) + else: + print(str(grid[i][j]) + " ", end="") + + +def solve_sudoku(): + # function to solve sudoku problem + global grid + for row in range(9): + # for loop to find 0 in the row and column + for col in range(9): + if grid[row][col] == 0: + # gives values to give in the spaces + for n in range(1, 10): + # if the number is valid or not + if is_number_valid(row, col, n): + # assigns the value + grid[row][col] = n + # recalls the function + solve_sudoku() + # if the number is already present then + # assign the value 0 to it + grid[row][col] = 0 + return + print("\nSoltion is:") + print_board() # calls the print_board function + # if user presses enter then function is called again + input("Check for more solution?") +# takes each line of the sudoku as input with spaces between two numbers + + +grid = [list(map(int, input().split()))[:9] for _ in range(9)] + + +solve_sudoku() # calls the solve function