N-Queens2 #2 OrTools로 n-Queens 솔루션 결과 출력 이전 글에서 orTools로 n-Queens 솔루션을 찾는 방법을 설명했습니다. 이번 글에서는 지난 번 글에 이어지는 글로 솔루션의 결과를 출력하는 방법을 소개합니다. ### 모델 해결 solver = cp_model.CpSolver() ### 모델 다이어그램 출력 printer = DiagramPrinter(queens) ### 모델 다이어그램 출력 status = solver.SearchForAllSolutions(model, printer) print() print('찾은 솔루션 개수 : %i' % printer.SolutionCount()) print("걸린 시간: ", solver.WallTime(), "ms") 지난 번 소스 맨 마지막 부분을 보면 solver를 통해 솔루션을 찾기에서 Diag.. 2022. 11. 21. #1 OrTools로 n-Queens 해답 찾기 N-Queens 문제는 가로, 세로 칸의 수가 동일한 체스판 모든 행에 서로 공격할 수 없는 퀸을 배치하는 문제입니다. 이 문제에 대한 자세한 정보는 위키백과에서 확인 할 수 있습니다. 아래는 OrTools 최적화 도구를 이용해 N-Queens 문제를 푸는 python 소스입니다. import sys # CP-SAT 솔버 from ortools.sat.python import cp_model def main(board_size): model = cp_model.CpModel() # 변수 생성 # 배열의 인덱스는 열(column), 값은 행(Row) queens = [model.NewIntVar(0, board_size - 1, 'x%i' % i) for i in range(board_size)] # 제약.. 2022. 11. 18. 이전 1 다음