, . , , . , . ,
4. :
(X Y), 1 8. :
X/Y
, , "/" , . . 4.5 .
12 3 4 5 6 7 8
. 4.5. . [1/4, 2/2, 3/7, 4/3. 5/6, 6/8, 7/5, 8/1]
,
[ tint, X2/Y2, X3/Y3 8/]
, . solution XI, Y1, 2, Y2.... 8, Y8. , , , , , . , X, , : [ l/Vl, 2/Y2, 3/Y3,..., S/YB]
8x8. . , , , . .
. , ( ) 8, , . solution , .
1. , . 2. [X/Y | Others]. |
. , ,
. :
I. Prolog
2 X/Y, , Others. , .
|
|
1. , Others, ; , Others .
2. X Y 1 8.
3. , X/Y, Others.
, , solution. : Y 1 8, .. (1,2,3,4,5,, 7, ]. , X, , X . , X 1 8. , noattack. , Prolog : solution([x/Y | Others]):-
solution! Others),
meiPberf Y, [1,2,3,4,5,6,7,8]),
noattack(X/Y, Others).
noattack: noattack; 0, Qliat)
, , .
1. Qlist , , , , , .
2. Qlist , (Ql | Qlistl] :
) , Q, ,
Q1;
) , Q,
Qlistl.
, , , , , : , . , , :
;
( ) ; , , , X Y.
4,2. . , . :?- template! S), solution r S).
4. :
;
:
S = [ 1/4, 2/2, 3/7, 4/3, 5/6, 6/8, 7/5, 8/1]; S = [ 1/5, 2/2, 3/4, 4/7, 5/3, 6/8, 7/6, 8/1]; S = [ 1/3, 2/5, 3/2, 4/8, 5/6, 6/4, 7/7, 8/13;
4.2, 1
% solution! BoardPositionj-,
% BoardPosition -
solution [ []).
solution) (X/Y I Others]):- % X/Y;
solution! others), * Others -
member; Y, [ 1, 2, 3, 4,5, 6, 7,8] >,
noattack(x/Y, Others). %
|
|
noattackf, _,[]). %
noattackf X/Y, [Xl/Yl I Others]):-
Y -- Yl, %
Y1-Y -- 1-, %
Y1-Y =4- -1, noattackf X/Y, Others).
member(Item, [Item I Rest]).
member[ Item, [First I Rest]):-member! Item, Rest).
%
template! [1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,B/YB]).