Commit 149d835c authored by Vít Novotný's avatar Vít Novotný
Browse files

added examples 3.3_5 and 3.6_17

parent 298095c8
% nacteni:
/* ['3.3_5.pl']. */
:- retractall(solution/1).
:- retractall(sol/1).
:- retractall(noattack/2).
:- retractall(template/1).
:- dynamic
solution/1,
sol/1,
noattack/2,
template/1.
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
% abychom se vyhli varovanim "Redefined static procedure ..."
:- dynamic
start/0.
start:-
write('PROBLEM OSMI DAM III - reseni pomoci seznamu volnych pozic'),nl,
write('Zadejte dotaz "solution(Solution)", po vypoctu prvniho reseni'),nl,
write('si dalsi vyzadate stisknutim ";".'),nl.
?-start.
:- retractall(start/0).
#!/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()))
PROBLEM OSMI DAM I
Volani next(solution()) : [1, 5, 8, 6, 3, 7, 2, 4]
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
from collections import deque
from linked_lists import Cons, Nil
def solution(node):
for path in breadth_first_search(node):
yield path
def breadth_first_search(start_node):
paths = deque([Cons(start_node, Nil)]) # pouzijeme pythonovskou frontu jako FIFO
while len(paths) > 0:
path = paths.popleft()
node = path.head
if is_goal(node):
yield path
for node1 in move_anyY(node):
paths.append(Cons(node1, path))
def is_goal(x):
# zavisi na resenem problemu
return x == "E"
graph = dict(A=["B", "E", "F"],
B=["C"], F=["C"],
C=["D"],
D=["E"])
def move_anyY(x):
# zavisi na resenem problemu
if x in graph:
for y in graph[x]:
yield y
# demonstracni vypis
if __name__ == "__main__":
print("Breadth-First Search")
print("Volani next(solution('A')) : %s" % next(solution('A')))
Breadth-First Search
Volani next(solution('A')) : ['E', 'A']
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment