0% found this document useful (0 votes)
89 views3 pages

#Sudoku

This document defines a Board class for solving Sudoku puzzles using backtracking. The Board class initializes a 2D table to represent the puzzle board with empty slots marked as 0. It tracks the number of free positions and a list of moves. Methods are defined to place values on the board (makeMove), remove values (unMakeMove), find valid candidates for the next move (constructCandidates), check for a complete solution (isASolution), and print the board state (printBoard). The backtrack function recursively tries values in each position, removing invalid choices until it finds a complete solution or exhausts all options.

Uploaded by

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

#Sudoku

This document defines a Board class for solving Sudoku puzzles using backtracking. The Board class initializes a 2D table to represent the puzzle board with empty slots marked as 0. It tracks the number of free positions and a list of moves. Methods are defined to place values on the board (makeMove), remove values (unMakeMove), find valid candidates for the next move (constructCandidates), check for a complete solution (isASolution), and print the board state (printBoard). The backtrack function recursively tries values in each position, removing invalid choices until it finds a complete solution or exhausts all options.

Uploaded by

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

class Board:

def __init__(self):
self.dim = 9
self.table = [[0 for j in range(self.dim+1)] for i in range(self.dim+1)]
self.freeCount = self.dim*self.dim
self.move = [() for i in range(self.freeCount)]
#print "partial soln after pop:", a
if finished:
#print "Returning because finished is set to True"
return
def isASolution(board):
return board.freeCount == 0
def processSolution(board):
global finished
#print "Solution:"
printBoard(board)
finished = True
#raw_input()
def printBoard(board):
for row in board.table[1:]:
print " ".join(map(str, row[1:]))
def makeMove(a,k,board):
value = a[k]
x,y = board.move[k]
#print "Inserting %d to position %d %d" % (value, x, y)
board.table[x][y] = value
board.freeCount -= 1
'''print "\nBoard position"
printBoard(board)
print "Free positions: %d" % board.freeCount
raw_input()'''
def unMakeMove(a,k,board):
x,y= board.move[k]
board.table[x][y] = 0
board.freeCount += 1
#print "Removing value from position %d %d" % (x, y)
#print "\nBoard position"
#printBoard(board)
#print "Free positions: %d" % board.freeCount
#raw_input()
def constructCandidates(a,k,board):
nxtSq = nextSquare(board)
if not nxtSq:
return ()
posVal = possibleValues(nxtSq, board)
if not posVal:
return ()
'''print "Move:", board.move
print "K:", k'''
board.move[k] = nxtSq
return posVal
def nextSquare(board):
minValues = 9

nxt = ()
for i in range(1,board.dim+1):
for j in range(1,board.dim+1):
if board.table[i][j] == 0:
posValues = localValues((i,j), board)
if len(posValues) < minValues:
minValues = len(posValues)
nxt = (i,j)
if minValues == 0:
return ()
return nxt
def localValues(pos, board):
x,y = pos
possibleValues = [True for i in range(board.dim+1)]
possibleValues[0] = False #zero index not in board
for i in range(1,board.dim+1):
val = board.table[x][i]
if val != 0:
possibleValues[val] = False
val = board.table[i][y]
if val != 0:
possibleValues[val] = False
rowStart, rowEnd = region(x)
colStart, colEnd = region(y)
'''print "position: %d %d" % (x,y)
print "Row start, end: %d %d", (rowStart, rowEnd)
print "Col start, end: %d %d", (colStart, colEnd)
raw_input()'''
for i in range(rowStart, rowEnd):
for j in range(colStart, colEnd):
val = board.table[i][j]
if val != 0:
#print "Removing %d from possible values since it is available a
lready in square %d %d" % (val, i, j)
possibleValues[val] = False
values = [i for i in range(board.dim+1) if possibleValues[i]]
'''print "Possible val array: ", possibleValues
print "Returning values:", values
raw_input()'''
return values
def possibleValues(pos, board):
x,y = pos
posValues = localValues(pos, board)
values = []
for val in posValues:
board.table[x][y] = val
if nextSquare:
values.append(val)
board.table[x][y] = 0
return values

def region(x):
if x<=3:
start = 1
end = 4
elif x<=6:

start = 4
end = 7
else:
start = 7
end = 10
return (start,end)
board = Board()
finished = False
for row in range(board.dim):
line = map(int, raw_input().strip().split())
board.table[row+1] = [0] + line
for val in line:
if val != 0:
board.freeCount -= 1
'''print
print "printing the initial board"
printBoard(board)'''
backtrack([], -1, board)

You might also like