Skip to content

Commit c7fea47

Browse files
authored
Merge pull request #327 from Tejas1510/sudoku
A sudoku Solver
2 parents 1e7cea7 + 14ae6a0 commit c7fea47

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

Python/SudokuSolver/Readme.MD

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Sudoku Solver
2+
3+
## Introduction
4+
```
5+
This is a python application which takes a matrix as input and gives the solved sudoku problem as the output.
6+
It is based on the backtracking algorithm.
7+
```
8+
## How to use it :
9+
1. Download or clone the repository
10+
2. Install Required Libraries
11+
3. Run sudoku.py
12+
5. you will be asked to give enter the complete sudoku as a matrix
13+
6. you will get the the solved sudoku as the input
14+
15+
## Output
16+
17+
![endpoint](https://github.com/Tejas1510/hacking-tools-scripts/blob/sudoku/Python/SudokuSolver/images/image1.png)
18+
19+
![endpoint](https://github.com/Tejas1510/hacking-tools-scripts/blob/sudoku/Python/SudokuSolver/images/image2.png)
20+
21+
![built with love](https://forthebadge.com/images/badges/built-with-love.svg)
22+
23+
Check out my Github profile [Tejas1510!](https://github.com/Tejas1510)

Python/SudokuSolver/images/image1.png

130 KB
Loading

Python/SudokuSolver/images/image2.png

147 KB
Loading

Python/SudokuSolver/sudoku.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#Take Matrix as input
2+
n=int(input(" Enter no of rows and columns of sudoku problem : "))
3+
print("Please enter a ",n,"x",n,"Matrix")
4+
matrix = [[0 for i in range(n)] for j in range(n)]
5+
for i in range(0,n):
6+
for j in range(0,n):
7+
matrix[i][j] = int(input())
8+
# solve matrix which bascially uses two function find_empty() and valid()
9+
def solve(matrix):
10+
find = find_empty(matrix)
11+
# if value of find is false
12+
if not find:
13+
return True
14+
else:
15+
row, col = find
16+
# looping through all rows of matrix
17+
for i in range(1,n+1):
18+
if valid(matrix, i, (row, col)):
19+
matrix[row][col] = i
20+
21+
if solve(matrix):
22+
return True
23+
24+
matrix[row][col] = 0
25+
return False
26+
def valid(matrix, num, pos):
27+
# Check row
28+
for i in range(len(matrix[0])):
29+
if matrix[pos[0]][i] == num and pos[1] != i:
30+
return False
31+
# Check column
32+
for i in range(len(matrix)):
33+
if matrix[i][pos[1]] == num and pos[0] != i:
34+
return False
35+
# Check box
36+
box_x = pos[1] // 3
37+
box_y = pos[0] // 3
38+
#looping over the complete matrix
39+
for i in range(box_y*3, box_y*3 + 3):
40+
for j in range(box_x * 3, box_x*3 + 3):
41+
if matrix[i][j] == num and (i,j) != pos:
42+
return False
43+
return True
44+
#function to print the complete sudoku
45+
def print_board(matrix):
46+
for i in range(len(matrix)):
47+
if i % 3 == 0 and i != 0:
48+
print("- - - - - - - - - - - - - ")
49+
50+
for j in range(len(matrix[0])):
51+
if j % 3 == 0 and j != 0:
52+
print(" | ", end="")
53+
54+
if j == 8:
55+
print(matrix[i][j])
56+
else:
57+
print(str(matrix[i][j]) + " ", end="")
58+
# function to find whether entered matrix is empty or not
59+
def find_empty(matrix):
60+
for i in range(len(matrix)):
61+
for j in range(len(matrix[0])):
62+
if matrix[i][j] == 0:
63+
return (i, j) # row, col
64+
return None
65+
print("The entered Matrix for sudoku solver is : ")
66+
print("")
67+
print_board(matrix)
68+
solve(matrix)
69+
print("")
70+
print("***********************************************")
71+
print("")
72+
print("The output is : ")
73+
print("")
74+
print_board(matrix)

0 commit comments

Comments
 (0)