#!/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(1, 9):
for y in range(1, 9):
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)
# demonstracni vypis
if __name__ == "__main__":
print("PROBLEM OSMI DAM I")
print("Volani next(solution()) : %s" % next(solution()))
PROBLEM OSMI DAM I
Volani next(solution()) : [(8, 4), (7, 2), (6, 7), (5, 3), (4, 6), (3, 8), (2, 5), (1, 1)]
| % nacteni:
/* ['3.1_3.pl']. */
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
start:-
write('PROBLEM OSMI DAM I'),nl,
write('Prvni reseni solution(Solution):'),nl,
solution(Solution),write(Solution),nl.
?-start.
PROBLEM OSMI DAM I
Prvni reseni solution(Solution):
[8/4,7/2,6/7,5/3,4/6,3/8,2/5,1/1]
|