-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from AkshayaPujitha/main
suduko solver gives solution to the suduko problem
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import numpy as np | ||
|
||
grid=[[1,0,4,2,0,7,9,8,0], | ||
[0,8,0,0,0,0,6,0,2], | ||
[6,0,0,8,0,3,0,0,7], | ||
[0,0,0,9,4,1,0,6,0], | ||
[3,0,0,0,0,0,0,0,9], | ||
[0,7,0,6,3,5,0,0,0], | ||
[4,0,0,5,0,2,0,0,1], | ||
[2,0,7,0,0,0,0,9,0], | ||
[0,5,9,1,0,4,3,0,6]] | ||
def possible(row,column,number): | ||
global grid | ||
#checks the row | ||
for i in range(0,9): | ||
if grid[row][i]==number: | ||
return False | ||
#checks the column | ||
for i in range(0,9): | ||
if grid[i][column]==number: | ||
return False | ||
row=row//3 | ||
column=column//3 | ||
if row==1: | ||
row=3 | ||
elif row==2: | ||
row=6 | ||
if column==1: | ||
column=3 | ||
elif column==2: | ||
column=6 | ||
#checks 3X3 square grid | ||
for i in range(row,row+3): | ||
for j in range(column,column+3): | ||
if grid[i][j]==number: | ||
return False | ||
|
||
return True | ||
|
||
def solve(): | ||
global grid | ||
for row in range(0,9): | ||
for column in range(0,9): | ||
if grid[row][column]==0: | ||
for number in range(1,10): | ||
if possible(row,column,number): | ||
grid[row][column]=number | ||
solve() | ||
grid[row][column]=0 | ||
return | ||
print(np.matrix(grid)) | ||
|
||
|
||
solve() | ||
|
||
|
||
|
||
|
||
|
||
|
||
|