File: knights.py

package info (click to toggle)
logilab-constraint 0.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 384 kB
  • ctags: 497
  • sloc: python: 2,902; xml: 165; makefile: 17
file content (114 lines) | stat: -rw-r--r-- 3,840 bytes parent folder | 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python2.3
# -*- coding: iso-8859-1 -*-
# copyright 2002-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of logilab-constraint.
#
# logilab-constraint is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 2.1 of the License, or (at your
# option) any later version.
#
# logilab-constraint is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with logilab-constraint. If not, see <http://www.gnu.org/licenses/>.
"""Knight tour problem:
Place n*n values on a checker so
that consecutive values are a knight's
move away from each other"""


from logilab.constraint import *
from logilab.constraint.distributors import *

def knight_tour(size=6,verbose=0):
    variables = []
    domains = {}
    constraints = []
    black_checker=[]
    # the black tiles
    white_checker=[]
    #the white tiles: one less if n is odd
    for row in range(size):
        for column in range(size):
            if (row+column)%2==0:
                black_checker.append((row,column))
            else:
                white_checker.append((row, column))

    # One variable for each step in the tour
    for i in range(size*size):
        name = 'x%02d'%i
        variables.append(name)
        # The knight's move jumps from black to white
        # and vice versa, so we make all the even steps black
        # and all the odd ones white.
        if i%2==0:
            domains[name] = fd.FiniteDomain(black_checker)
        else:
            domains[name] = fd.FiniteDomain(white_checker)
        if i > 0:
            j = i -1
            k1 = 'x%02d'%j
            k2 = 'x%02d'%i
            # the knight's move constraint
            c = fd.make_expression((k1,k2),
                       'abs(%(v1)s[0]-%(v2)s[0]) + abs(%(v1)s[1]-%(v2)s[1]) == 3'%\
                       {'v1':k1,'v2':k2})
            constraints.append(c)
            c = fd.make_expression((k1,k2),
                       'abs(abs(%(v1)s[0]-%(v2)s[0]) - abs(%(v1)s[1]-%(v2)s[1])) == 1'%\
                       {'v1':k1,'v2':k2})
            constraints.append(c)
    constraints.append(fd.AllDistinct(variables))
    half = size/2
    r = Repository(variables,domains,constraints)
    sol = Solver(EnumeratorDistributor()).solve_one(r,verbose)	
    return sol

def draw_solution(sol, size):
    # change the keys into elements, elements into keys
    # to display the results.
    # I'm sure there's a better way to do this, but I'm
    # new to python
    board = ''
    board += '_'*(size*3+1)+'\n'
    squares = {}
    for t in sol.items():
        squares[(t[1][0]*size)+t[1][1]]=t[0]
    for i in range(size):
        for j in range(size):
        # find the variable whose value is (i,j)
            square = squares[i*size+j]
            # numbering should start from 1 ,not 0
            intsquare = int(square[1:4]) + 1
            board+='|%02s'%intsquare
        board+='|\n'
    board += ''*(size*3+1)+'\n'
    print board


if __name__ == '__main__':
    import sys,getopt
    opts,args = getopt.getopt(sys.argv[1:],'dv')
    display = 0
    verbose = 0
    if args:
        size = int(args[0])
    else:
        size = 6
    for o,v in opts:
        if o == '-d':
            display = 1
        elif o == '-v':
            verbose += 2
    count = 0
    sol = knight_tour(size,verbose)
    if display:
        print 'Solution found:'
        draw_solution(sol,size)