def solve_puzzle(puzzle, argv): """Solve the sudoku puzzle.""" strPuzzle = [] dummyPuzzle = [] solved = False strPuzzle = [map(str, x) for x in puzzle] global row_len row_len = range(len(strPuzzle)) global col_len col_len = range(len(strPuzzle[0])) if len(argv) > 1 and argv[1] == "backtracking": dummyPuzzle, solved = backtrack.solve() else: [suggestValue(strPuzzle,row,col,i) for i in range(2) for row in row_len for col in col_len if strPuzzle[row][col] == '0'] startR, startC = nextEntry(strPuzzle, True) dummyPuzzle = copy.deepcopy(strPuzzle) dummyPuzzle , solved = search(dummyPuzzle, strPuzzle, startR, startC) result = [] if solved: display(dummyPuzzle) for x in dummyPuzzle: temp = map(int, x) result.append(temp) return result return load_sudoku('given_solution.txt')
def generateSolvedBoard(): # 9x9 matrix of 0's board = [[0 for _ in range(9)] for _ in range(9)] # pick 11 random places, places random valid numbers there for _ in range(11): while True: row, col = (randrange(9) for _ in range(2)) val = randrange(1, 10) if validMove(board, row, col, val): break board[row][col] = val # solve the rest of the board solve(board) return board
def isUnique(b, r, c): val = b[r][c] for i in [j for j in range(1, 10) if j != val]: if validMove(b, r, c, i): bc = deepcopy(b) bc[r][c] = i if solve(bc, 0): return False return True
def solve(self): self.img2digits() if not isSolvable(self.puzzle): self.solution = self.puzzle print ("Not Solvable") self.writeCellsOut() return False solved, complete_puzzle = solve(self.puzzle) if solved: print ("Solved!") self.solution = complete_puzzle else: print ("Cant find solution") self.writeCellsOut() self.solution = self.puzzle return solved
def solve(self, src, filename): world_obj = world.World.from_string(src) _, solution = backtrack.solve(world_obj, time_limit) return solution
[3, 8, 9, 2, 4, 5, 6, 1, 7]] grid = grid1 grid_sol = grid1_sol #method = "backtrack" #method = "reverse backtrack" #method = "backtrack with implications" method = "backtrack with forward checking and MRV" #method = "best first search" #method = "crook" start = time.time() if method == "backtrack": backtracks = backtrack.solve(grid, "1") elif method == "reverse backtrack": backtracks = backtrack.solve(grid, "2") elif method == "backtrack with implications": backtracks = backtrack.solve(grid, "3") elif method == "backtrack with forward checking and MRV": backtracks = backtrack.solve(grid, "4") elif method == "crook": crook.solve(grid) elif method == "best first search": grid = bestfirstsearch.solve(grid) end = time.time() print("Solution: ") print_grid(grid)
BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 128, 0) CRIMSON = (220, 20, 60) LOCKEDCELLCOLOUR = (255, 0, 255) #boards board = [[0, 2, 0, 0, 4, 0, 8, 0, 0], [0, 0, 3, 0, 0, 1, 0, 5, 0], [8, 0, 1, 7, 0, 6, 3, 4, 9], [0, 0, 5, 0, 0, 9, 1, 0, 2], [2, 1, 0, 0, 0, 8, 0, 6, 4], [0, 0, 0, 0, 6, 0, 5, 0, 0], [5, 6, 0, 9, 1, 3, 4, 0, 0], [0, 4, 2, 6, 0, 0, 0, 1, 0], [1, 0, 7, 0, 0, 0, 6, 0, 3]] restore_board = [[0, 2, 0, 0, 4, 0, 8, 0, 0], [0, 0, 3, 0, 0, 1, 0, 5, 0], [8, 0, 1, 7, 0, 6, 3, 4, 9], [0, 0, 5, 0, 0, 9, 1, 0, 2], [2, 1, 0, 0, 0, 8, 0, 6, 4], [0, 0, 0, 0, 6, 0, 5, 0, 0], [5, 6, 0, 9, 1, 3, 4, 0, 0], [0, 4, 2, 6, 0, 0, 0, 1, 0], [1, 0, 7, 0, 0, 0, 6, 0, 3]] solved_board = [[0, 2, 0, 0, 4, 0, 8, 0, 0], [0, 0, 3, 0, 0, 1, 0, 5, 0], [8, 0, 1, 7, 0, 6, 3, 4, 9], [0, 0, 5, 0, 0, 9, 1, 0, 2], [2, 1, 0, 0, 0, 8, 0, 6, 4], [0, 0, 0, 0, 6, 0, 5, 0, 0], [5, 6, 0, 9, 1, 3, 4, 0, 0], [0, 4, 2, 6, 0, 0, 0, 1, 0], [1, 0, 7, 0, 0, 0, 6, 0, 3]] solved_board = backtrack.solve(solved_board) #position and sizes gridPos = (75, 75) cellsize = 50 gridSize = cellsize * 9