4.6, , 4,2, Y . ? ? , ,
4.5.2, 2
1 :
[ l/Yi, 2/Y2, 3/Y3,..., 8/YS]
. X , - . , Y , :
[ XI, Y2, Y3,..., YB]
. Y. -
I, Prolog
1, 2,..., 8. . : [1,2,3,4,5, 6,7,]
, S, , . : solution (S): -
permutation ([1,2,3,4,5,6,7,8], S),
safe [ S).
permutation 3, safe. .
1, S . , , ,
- .
2. S - [Queen | Others].
, Others Queen
- Others.
Prolog1 : safe ([ ]).
safe. [Queen I Others] ]: -safe: Others), noattack< Queen, others).
noattack . , Y, X . noattack, . 4.6, : noattack< Queen, Others)
, Queen Others, X Queen Others 1. X Queen Others. noattack : oeattacfel Queen, Others, Xdist)
noattack safe : noattack(Queen, others, 1)
|
|
) |
Queen - |
/ *\ | |
J | |
/ | - |
---/ | |
i | r |
\t | / |
X 1
Others
6) | |
/ | |
/ ' | |
/ 1 | |
f / | |
: | / |
. _^ | ' |
i |
X 3
. 4.6. noattack: ) X Queen Other 1; ) X Queen Others 3
4. :
noattack , Others: Others , , , , , ; Others , Queen Others ( Xdist Queen), , Others, Xdist + 1. , 4.3.
4.3. 2
* solution) Queens),
% Queens - Y
solution Queens):-
permutation([1,2,3,4,5,6,7,8!, Queens), safe(Queens).
permutation((], []).
permutation[ [Head I Tail], PermList):-
permutation; Tail, PermTail),
dell Head, PermList, PermTail). ft Head Tail,
%
% del(Item, List, NewList) - NewList
% Item List
del t Item, [Item I List], List).
dell Item, [First I List], [First I Listl]):-del(Item, List, Listl).
* safe i Queens),
Queens - Y
safe([]).
safe([Queen I Others] }:- safe(Others), noattack(Queen, Others, 1).
noattackl _, [], _).
noattack! Y, [Yl I Ylist], Xdist):-Yl-Y -\- Xdist, y-yi -\- xdist,
Distl is Xdist + 1, noattack (Y, Ylist, Distl).