return min_row, min_col def constraining_value(row, col, board, assignment): count = 0; for i in range(board.BoardSize): if not sudoku.is_assigned(row, i, board, assignment): count += 1 if not sudoku.is_assigned(i, col, board, assignment): count += 1 # print count subsquare = int(math.sqrt(board.BoardSize)) squareRow = row // subsquare squareCol = col // subsquare # return count for r in range(subsquare): for c in range(subsquare): if not sudoku.is_assigned(r, c, board, assignment): count += 1 # print count return count # Test code for a file with a board test_board = sudoku.parse_file('test9.txt') tboard = sudoku.SudokuBoard(len(test_board),test_board) tboard.print_board() backtracking_search_forward_checking_mrv(tboard) tboard.print_board() print variable_assignment_count[0]
# subsquare checking subsquare = int(math.sqrt(board.BoardSize)) squareRow = row // subsquare squareCol = col // subsquare for r in range(subsquare): for c in range(subsquare): if ( not sudoku.is_assigned(squareRow * subsquare + r, squareCol * subsquare + c, board, assignment) ) and domains[str([squareRow * subsquare + r, squareCol * subsquare + c])].count(val): count += 1 dictionary[val] = count sorted_dictionary = sorted(dictionary.iteritems(), key=operator.itemgetter(1)) dom = [] for dom_val in sorted_dictionary: dom.append(dom_val[0]) return reversed(dom) # Test code for a file with a board test_board = sudoku.parse_file("test25.txt") tboard = sudoku.SudokuBoard(len(test_board), test_board) tboard.print_board() backtracking_search_forward_checking_mrv_mcv_lcv(tboard) tboard.print_board() print variable_assignment_count[0]