⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fig4_9.pl

📁 超多的prolog源代码 具体内容见压缩包里面的programs.txt
💻 PL
字号:
% Figure 4.9   Program 2 for the eight queens problem.


% solution( Queens) if 
%   Queens is a list of Y-coordinates of eight non-attacking queens

solution( Queens)  :-
   permutation( [1,2,3,4,5,6,7,8], Queens),
   safe( Queens). 

permutation( [], [] ). 

permutation( [Head | Tail], PermList)  :-
   permutation( Tail, PermTail),
   del( Head, PermList, PermTail).   % Insert Head in permuted Tail 

% del( Item, List, NewList): deleting Item from List gives NewList

del( Item, [Item | List], List). 

del( Item, [First | List], [First | List1] )  :-
   del( Item, List, List1). 

% safe( Queens) if 
%   Queens is a list of Y-coordinates of non-attacking queens

safe( [] ). 

safe( [Queen | Others] )  :-
   safe( Others),
   noattack( Queen, Others, 1). 

noattack( _, [], _). 

noattack( Y, [Y1 | Ylist], Xdist)  :-
   Y1-Y =\= Xdist,
   Y-Y1 =\= Xdist,
   Dist1 is Xdist + 1,
   noattack( Y, Ylist, Dist1).

⌨️ 快捷键说明

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