Stáhnout: 3.3_5.py   Zobrazit: jednoduše   3.3_5.pl

#!/usr/bin/env python
# encoding=utf-8 (pep 0263)

from linked_lists import LinkedList, Cons, Nil

def solution():
    for xs in sol(LinkedList(range(1, 9)), LinkedList(range(1, 9)),
                  LinkedList(range(-7, 8)), LinkedList(range(2, 17))):
        yield xs

def sol(dx, dy, du, dv):
    if dx == Nil:
        if dy == Nil:
            yield Nil # umistili jsme osm dam
        else:
            return # umistili jsme mene nez osm dam
    else:
        x = dx.head
        for y, dy1 in del_anyX(dy):
            for du1 in del_(du, x - y):
                for dv1 in del_(dv, x + y):
                    for others in sol(dx.tail, dy1, du1, dv1):
                        yield Cons(y, others)

def del_(xs, x):
    if xs == Nil:
        return
    if x == xs.head:
        yield xs.tail
    else:
        for ys in del_(xs.tail, x):
            yield Cons(xs.head, ys)

def del_anyX(xs):
    if xs == Nil:
        return
    yield xs.head, xs.tail
    for y, ys in del_anyX(xs.tail):
        yield y, Cons(xs.head, ys)

# demonstracni vypis
if __name__ == "__main__":
    print("PROBLEM OSMI DAM I")
    print("Volani next(solution()) : %s" % next(solution()))

% nacteni:
/* ['3.3_5.pl']. */

solution(YList) :- sol(YList ,[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8],
                       [-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7],
                       [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]).
sol([],[], _Dy,_Du,_Dv).
sol([Y|YList],[X|Dx1],Dy,Du,Dv) :- del(Y,Dy,Dy1), U is X-Y, del(U,Du,Du1), V is X+Y,
                                   del(V,Dv,Dv1), sol(YList,Dx1,Dy1,Du1,Dv1).
del(Item,[ Item|List ], List ).
del(Item,[ First | List ],[ First | List1 ]) :- del(Item,List , List1 ).

% demonstracni vypis

start:- 
    write('PROBLEM OSMI DAM III - reseni pomoci seznamu volnych pozic'),nl,
    write('Prvni reseni solution(Solution):'),nl,
    solution(Solution),write(Solution),nl.
?-start.


 Stáhnout: 3.3_5.py   Zobrazit: jednoduše   3.3_5.pl