computer assignment needed by today
Was this answer helpful? 0 1
home / study / engineering / computer science / computer science questions and answers / solve the defective chessboard problem using dynami…
…
Solve the Defective Chessboard Problem using Dynamic Programming, with the board size 2n x 2n.
The Code must be written in Python.
Professor answered this 93 answers
SOURCE CODE:
from __future__ import print_function import sys from ortools.sat.python import cp_model def main(board_size): model = cp_model.CpModel() # Creates the variables. # The array index is the column, and the value is the row. queens = [model.NewIntVar(0, board_size - 1, 'x%i' % i) for i in range(board_size)] # Creates the constraints. # The following sets the constraint that all queens are in different rows. model.AddAllDifferent(queens) # Note: all queens must be in different columns because the indices of queens are all different. # The following sets the constraint that no two queens can be on the same diagonal. for i in range(board_size): # Note: is not used in the inner loop. diag1 = [] diag2 = [] for j in range(board_size): # Create variable array for queens(j) + j. q1 = model.NewIntVar(0, 2 * board_size, 'diag1_%i' % i) diag1.append(q1) model.Add(q1 == queens[j] + j) # Create variable array for queens(j) - j. q2 = model.NewIntVar(-board_size, board_size, 'diag2_%i' % i) diag2.append(q2) model.Add(q2 == queens[j] - j) model.AddAllDifferent(diag1) model.AddAllDifferent(diag2) ### Solve model. solver = cp_model.CpSolver() solution_printer = SolutionPrinter(queens) status = solver.SearchForAllSolutions(model, solution_printer) print() print('Solutions found : %i' % solution_printer.SolutionCount()) class SolutionPrinter(cp_model.CpSolverSolutionCallback): """Print intermediate solutions.""" def __init__(self, variables): cp_model.CpSolverSolutionCallback.__init__(self) self.__variables = variables self.__solution_count = 0 def OnSolutionCallback(self): self.__solution_count += 1 for v in self.__variables: print('%s = %i' % (v, self.Value(v)), end = ' ') print() def SolutionCount(self): return self.__solution_count
Question: Solve the Defective Chessboard Problem using Dynamic Prog See this question in the app
Expert Answer
My Textbook Solutions
Nature of Mathematics 13th Edition
Nature of Mathematics 13th Edition
Nature of Mathematics 12th Edition
View all solutions
Computer Science Cheg tutors who can help righ now
Matthew Z. University of Colora…
Yiyang L. Arizona State Unive…
Pavel . Southern Methodist…
Find me a tutor
Post a question Answers from our experts for your toug homework questions
Enter question
Continue to post 0 questions remaining
Snap a photo from you phone to post a questio We'll send you a one-time do link
888-888-8888 Text
By providing your phone number, you agree to receiv automated text message with a link to get the app. St messaging rates may apply.
Find solutions for your homework Search
Study PackTextbook Solutions Expert Q&A Practice NEW!
class DiagramPrinter(cp_model.CpSolverSolutionCallback): def __init__(self, variables): cp_model.CpSolverSolutionCallback.__init__(self) self.__variables = variables self.__solution_count = 0 def OnSolutionCallback(self): self.__solution_count += 1 for v in self.__variables: queen_col = int(self.Value(v)) board_size = len(self.__variables) # Print row with queen. for j in range(board_size): if j == queen_col: # There is a queen in column j, row i. print("Q", end=" ") else: print("_", end=" ") print() print() def SolutionCount(self): return self.__solution_count if __name__ == '__main__': # By default, solve the 8x8 problem. board_size = 8 if len(sys.argv) > 1: board_size = int(sys.argv[1]) main(board_size)
Comment
ABOUT CHEGG
LEGAL & POLICIES
CHEGG PRODUCTS AND SERVICES
CHEGG NETWORK
CUSTOMER SERVICE
© 2003-2020 Chegg Inc. All rights reserved.
Study PackTextbook Solutions Expert Q&A Practice NEW!