Python: Recursion problems -
i trying make sudoku solver solves boards quickly. @ moment solver works on easy boards never terminates on harder boards. believe has recursion because easy boards not require recursion , hard boards do. appreciated.
import sys def rowno(i): return // 9 def colno(i): return % 9 def boxno(i): return (i // 9 // 3 )*3 + (i // 3) % 3 def isneighbor(i, j): if rowno(i) == rowno(j) or colno(i) == colno(j) or boxno(i) == boxno(j): return true else: return false def getfilename(): if sys.platform == "win32": filename = input("filename? ") else: filename = sys.argv[-1] return filename solutionlist = [] class board(object): def __init__(self, puzzle): self.puzzle = puzzle self.board = [cell(int(value), idx) idx, value in enumerate(puzzle)] self.change = false def printall(self): print [cell.candidates cell in self.board] #return str(" ") def update(self): self.change = false l = [cell cell in self.board if len(cell.candidates) == 1] in l: j in xrange(81): if isneighbor(i.dex, j) , i.dex != j: old = self.board[j].candidates self.board[j].delcandidate(i.value) if len(old) != len(self.board[j].candidates): self.change = true def tostring(self): str1 = ''.join(str(e.value) e in self.board) return str1 def solved(self): cell in self.board: if len(cell.candidates) != 1: return false return true def solve(self): self.change = true while self.change == true: self.update() if self.solved(): solutionlist.append(self.board) return l = [cell cell in self.board if len(cell.candidates) > 1] in l: j in i.candidates: newboard = board(self.tostring()) curlen = 12 curcell = -1 u in l: if len(u.candidates)<curlen: curlen=len(u.candidates) curcell = u.dex c in newboard.board[curcell].candidates: newboard.board[curcell].candidates = [int(c)] newboard.board[curcell].value = int(c) newboard.solve() return def __repr__(self): l = [cell.value cell in self.board] return str(l) class cell(object): def __init__(self, value, dex): self.value = value self.dex = dex if value == 0: self.candidates = [1,2,3,4,5,6,7,8,9] else: self.candidates = [int(value)] def __str__(self): return str(self.value) def delcandidate(self, value): # deletes value candidate list #return self.candidate.remove(value); self.candidates = [x x in self.candidates if x != value] if len(self.candidates) == 1: self.value = self.candidates[0] easy = "700583006006001405052006083300200958500078060648010300060802500003150072215600030" twosol = "000805200800000401705040009000100702040000000006430000030900000010006080000000000" hard = "040090008000000070060000120030020000005839060080600700050170600000043000003000200" #easy solution: 794583216836721495152496783371264958529378164648915327967832541483159672215647839 b = board(hard) print b b.solve() print "end of line" in solutionlist: print [cell.value cell in i] print "\n"
one major issue line for in l: in solve method. since you're recursing, need fill in 1 cell - recursion take care of rest. instead of for in l:, recurse on 1 cell best candidate (curcell):
l = [cell cell in self.board if len(cell.candidates) > 1] if len(l) > 0: newboard = board(self.tostring()) curlen = 12 curcell = -1 u in l: if len(u.candidates)<curlen: curlen=len(u.candidates) curcell = u.dex c in newboard.board[curcell].candidates: newboard.board[curcell].candidates = [int(c)] newboard.board[curcell].value = int(c) newboard.solve()
Comments
Post a Comment