Download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from Numberjack import *
 
# N-Queens
 
# The N-Queens problem is the probelm of placing N queens on an N x N chess
# board such that no two queens are attacking each other. A queen is attacking
# another if it they are on the same row, same column, or same diagonal.
 
 
def get_model(N):
    queens = VarArray(N, N)
    model = Model(
        AllDiff(queens),
        AllDiff([queens[i] + i for i in range(N)]),
        AllDiff([queens[i] - i for i in range(N)])
    )
    return queens, model
 
 
def solve(param):
    queens, model = get_model(param['N'])
    solver = model.load(param['solver'])
    solver.setHeuristic(param['heuristic'], param['value'])
    solver.setVerbosity(param['verbose'])
    solver.setTimeLimit(param['tcutoff'])
    solver.solve()
 
    out = ''
    if solver.is_sat() and param['print'] == 'yes':
        out += print_chessboard(queens)
    out += ('\nNodes: ' + str(solver.getNodes()))
    return out
 
 
def print_chessboard(queens):
    out = '+---' * len(queens) + '+\n'
    for queen in queens:
        out += ('|   '*queen.get_value()+'| Q |'+'   |'*(len(queens)-1-queen.get_value())+'\n'+'+---'*len(queens)+'+\n')
    return out
 
 
default = {'solver': 'Mistral', 'N': 6, 'heuristic': 'MinDomainMinVal',
           'print': 'yes', 'value': 'Lex', 'verbose': 0, 'tcutoff': 30}
 
 
if __name__ == '__main__':
    param = input(default)
    print solve(param)