def mainLoopmC(): while True: state = None state = read()['puzzle'] assignment = mostConstrainingSearch(state) temp = assignAll(assignment, state) # the final solution as a 2d list print('the answer is:') printPuzzle(temp)
def mainLoop(): while True: state = None state = read()['puzzle'] assignment = backtracking(state) temp = assignAll(assignment, state) # the final solution as a 2d list print('the answer is:') printPuzzle(temp)
def solve_puzzle(): while True: puzzle=read()['puzzle'] result,num_nodes=arc_consistent(puzzle) print() if result == None: print('no solution found') else: print('the answer is:') printPuzzle(result) print('Number of Nodes Visited: ' + str(num_nodes))
def investigate(): puzzle = read()['puzzle'] list1 = initialize() for i in range(len(list1)): t_initial = timeit.default_timer() solution, list1[i]['numVisited'] = list1[i]['function'](puzzle) list1[i]['time'] = timeit.default_timer() - t_initial print('{} solves the puzzle'.format(list1[i]['name'])) print('{} nodes visited'.format(list1[i]['numVisited'])) print('the answer is:') printPuzzle(solution) for dict1 in list1: time = dict1['time'] name = dict1['name'] num = dict1['numVisited'] print('{} solved the puzzle in {} s. {} nodes were visited'.format( name, time, num))
def backtrackingHelper(A,U,state): current = assignAll(A, state) printPuzzle(current) if rule.solutionCheck(current): # all unassigned cell have been assigned return A # and the number constraint is satisfied elif len(U) > 0: (i, j) = U.pop(0) for option in ['b', '_']: # one coordinate has 2 possible assignment B = A.copy() B[(i, j)] = option if ((i, j) not in rule.goodCells(current)) and option == 'b': continue temp = assignAll(B, state) if not rule.numberConstrain(temp): continue # recursive call with new assignments B result = backtrackingHelper(B, U.copy(), state) if result is not None: # WE HAVE no solution in this case return result return None
def mostConstrainingSearchHelper(A, U, state): current = assignDict(A, state) # the current state printPuzzle(current) if rule.solutionCheck(current): # all unassigned cell have been assigned return A # and the number constraint is satisfied elif len(U) > 0: # get the most constraining coordinate (i, j) = mostConstrainingCell(current, U) U.remove((i, j)) for option in ['b', '_']: # one coordinate has 2 possible assignment B = A.copy() B[(i, j)] = option if ((i, j) not in rule.goodCells(current)) and option == 'b': continue temp = assignDict(B, state) if not rule.numberConstrain(temp): continue # recursive call with new assignments B result = mostConstrainingSearchHelper(B, U.copy(), state) if result is not None: # WE HAVE no solution in this case return result return None