0% found this document useful (0 votes)
33 views6 pages

Aia Exp-2 Vu22eece0100102

The document presents a Python implementation of Sudoku solvers for both 9x9 and 5x5 grids. It includes functions to check if a number can be placed in a specific position and to recursively solve the puzzle. The results demonstrate cases where solutions exist and where they do not.

Uploaded by

myarrams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

Aia Exp-2 Vu22eece0100102

The document presents a Python implementation of Sudoku solvers for both 9x9 and 5x5 grids. It includes functions to check if a number can be placed in a specific position and to recursively solve the puzzle. The results demonstrate cases where solutions exist and where they do not.

Uploaded by

myarrams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Y.

Ram Prasad
VU22EECE0100102
AIA lab Observation
Experiment - 2

SUDOKU SOLVER
1. 9x9 Sudoku Solver
M = 9

def puzzle(a):
for i in range(M):
for j in range(M):
print(a[i][j],end =" ")
print()

def solve(grid,row,col,num):
for x in range(9):
if grid[row][x] == num:
return False

for x in range(9):
if grid[x][col] == num:
return False
startRow = row - row %3
startCol = col - col %3
for i in range(3):
for j in range(3):
if grid[i + startRow][j + startCol] == num:
return False
return True

def Suduko(grid, row, col):


if (row == M - 1 and col == M):
return True
if col == M:
row += 1
col = 0
if grid[row][col] > 0:
return Suduko(grid, row, col + 1)
for num in range(1, M + 1, 1):
if solve(grid, row, col, num):
grid[row][col] = num
if Suduko(grid, row, col + 1):
return True
grid[row][col] = 0
return False

grid = [[2, 5, 0, 0, 3, 0, 9, 0, 1],


[0, 1, 0, 0, 0, 4, 0, 0, 0],
[4, 0, 7, 0, 0, 0, 2, 0, 8],
[0, 0, 5, 2, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 9, 8, 1, 0, 0],
[0, 4, 0, 0, 0, 3, 0, 0, 0],
[0, 0, 0, 3, 6, 0, 0, 7, 2],
[0, 7, 0, 0, 0, 0, 0, 0, 3],
[9, 0, 3, 0, 0, 0, 6, 0, 4]]

print(" The input puzzle is: \n")


for i in grid:
print(i)
print("\n")
if (Suduko(grid, 0, 0)):
print("The Solution is given below: ")
puzzle(grid)
print("\n")

else:
print("Solution does not exist:(")

RESULT:
CASE 1: SOLUTION DOESN’T EXIST
The input puzzle is:

[1, 5, 0, 0, 3, 0, 9, 0, 2]

[0, 1, 0, 0, 0, 4, 0, 0, 0]

[4, 0, 7, 0, 0, 0, 2, 0, 8]

[0, 0, 5, 2, 0, 0, 0, 0, 0]

[0, 0, 0, 0, 9, 8, 1, 0, 0]

[0, 4, 0, 0, 0, 3, 0, 0, 0]

[0, 0, 0, 3, 6, 0, 0, 7, 5]

[0, 7, 0, 0, 0, 0, 0, 0, 3]

[9, 0, 3, 0, 0, 0, 6, 0, 4]

Solution does not exist:(


Case2: Perfect Solution
The input puzzle is:

[0, 0, 0, 2, 6, 0, 7, 0, 1]

[6, 8, 0, 0, 7, 0, 0, 9, 0]

[1, 9, 0, 0, 0, 4, 5, 0, 0]

[8, 2, 0, 1, 0, 0, 0, 4, 0]

[0, 0, 4, 6, 0, 2, 9, 0, 0]

[0, 5, 0, 0, 0, 3, 0, 2, 8]

[0, 0, 9, 3, 0, 0, 0, 7, 4]

[0, 4, 0, 0, 5, 0, 0, 3, 6]

[7, 0, 3, 0, 1, 8, 0, 0, 0]

The Solution is given below:


4 3 5 2 6 9 7 8 1
6 8 2 5 7 1 4 9 3
1 9 7 8 3 4 5 6 2
8 2 6 1 9 5 3 4 7
3 7 4 6 8 2 9 1 5
9 5 1 7 4 3 6 2 8
5 1 9 3 2 6 8 7 4
2 4 8 9 5 7 1 3 6
7 6 3 4 1 8 2 5 9

2. 5x5 Sudoku solver


M = 5

def puzzle(a):

for i in range(M):

for j in range(M):

print(a[i][j], end=" ")


print()

def solve(grid, row, col, num):

for x in range(M):

if grid[row][x] == num:

return False

for x in range(M):

if grid[x][col] == num:

return False

# Define regions manually for a 5x5 grid

region = [

(0, 0, 0, 1, 1),

(0, 1, 2, 2, 3),

(1, 2, 3, 4, 4)

startRow = row // 3

startCol = col // 3

for i in range(3):

for j in range(3):

regionRow = region[startRow][i]

regionCol = region[startCol][j]

if grid[regionRow][regionCol] == num:

return False

return True

def Suduko(grid, row, col):


if (row == M - 1 and col == M):

return True

if col == M:

row += 1

col = 0

if grid[row][col] > 0:

return Suduko(grid, row, col + 1)

for num in range(1, M + 1, 1):

if solve(grid, row, col, num):

grid[row][col] = num

if Suduko(grid, row, col + 1):

return True

grid[row][col] = 0

return False

grid = [

[0, 0, 0, 2, 0],

[0, 0, 0, 0, 3],

[0, 0, 0, 1, 0],

[0, 3, 0, 0, 0],

[2, 0, 0, 0, 0]

print("The input puzzle is: \n")

for i in grid:

print(i)

print("\n")

if (Suduko(grid, 0, 0)):
print("The Solution is given below: ")

puzzle(grid)

print("\n")

else:

print("Solution does not exist :(")

The input puzzle is:

[0, 0, 0, 2, 0]

[0, 0, 0, 0, 3]

[0, 0, 0, 1, 0]

[0, 3, 0, 0, 0]

[2, 0, 0, 0, 0]

Solution does not exist :(

You might also like