fig4_7.pl

来自「超多的prolog源代码 具体内容见压缩包里面的programs.txt」· PL 代码 · 共 30 行

PL
30
字号
%  Figure 4.7  Program 1 for the eight queens problem.


% solution( BoardPosition) if
%   BoardPosition is a list of non-attacking queens

solution( [] ).

solution( [X/Y | Others] )  :-      % First queen at X/Y, other queens at Others
  solution( Others),
  member( Y, [1,2,3,4,5,6,7,8] ),
  noattack( X/Y, Others).           % First queen does not attack others

noattack( _, [] ).                  % Nothing to attack

noattack( X/Y, [X1/Y1 | Others] )  :-
  Y =\= Y1,                         % Different Y-coordinates
  Y1-Y =\= X1-X,                    % Different diagonals
  Y1-Y =\= X-X1,
  noattack( X/Y, Others).

member( Item, [Item | Rest] ).

member( Item, [First | Rest] )  :-
  member( Item, Rest).

% A solution template

template( [1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,8/Y8] ).

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?