Commit 23adc4c7 authored by Vít Novotný's avatar Vít Novotný
Browse files

added example 3.1_3

parent d3fd3ff6
% nacteni:
/* ['3.1_3.pl']. */
:- retractall(solution/1).
:- retractall(sol/1).
:- retractall(noattack/2).
:- retractall(template/1).
:- dynamic
solution/1,
sol/1,
noattack/2,
template/1.
solution(S) :- template(S), sol(S).
sol([]).
sol([X/Y|Others]) :- sol(Others),
member(X,[1,2,3,4,5,6,7,8]),
member(Y,[1,2,3,4,5,6,7,8]),
noattack(X/Y,Others).
noattack(_,[]).
noattack(X/Y,[X1/Y1|Others]) :- X=\=X1, Y=\=Y1, Y1-Y=\=X1-X, Y1-Y=\=X-X1,
noattack(X/Y,Others).
template([_X1/_Y1, _X2/_Y2, _X3/_Y3, _X4/_Y4, _X5/_Y5, _X6/_Y6, _X7/_Y7, _X8/_Y8]).
% demonstracni vypis
% abychom se vyhli varovanim "Redefined static procedure ..."
:- dynamic
start/0.
start:-
write('PROBLEM OSMI DAM I'),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 Cons, Nil
def solution(n=8):
if n == 0:
yield Nil
else:
for others in solution(n-1):
for x in range(8):
for y in range(8):
if noattack(x, y, others):
yield Cons((x, y), others)
def noattack(x, y, others):
if others == Nil:
return True
else:
x1, y1 = others.head
return x != x1 and y != y1 and y1-y != x1-x and y1-y != x-x1 and \
noattack(x, y, others.tail)
def tisknireseni(reseni):
return ["%d/%d" % (x+1, y+1) for (x, y) in reseni]
# demonstracni vypis
if __name__ == "__main__":
print("PROBLEM OSMI DAM I")
print("Volani tisknireseni(next(solution())) : %s" % \
tisknireseni(next(solution())))
PROBLEM OSMI DAM I
Volani tisknireseni(next(solution())) : ['8/4', '7/2', '6/7', '5/3', '4/6', '3/8', '2/5', '1/1']
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