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

📄 queens3.pl

📁 PRl教学程序 PRl教学程序 PRl教学程序
💻 PL
字号:
%   File   : queens.pl
%   Author : Neng-Fa ZHOU
%   Date   : 2000
%   Purpose: A linear-space program for solving the N-queens problem 


go:-
    write('N=?'),read(N),queens(N).

queens(N):-
    statistics(runtime,[Start|_]),
    once(N),
    statistics(runtime,[End|_]),
    T is End-Start,
    write('%execution time ='), write(T), write(' milliseconds'),nl.

once(N):-
    fd_vector_min_max(0,N), % set the size of bit vectors
    make_list(N,List),
    domain(List,1,N),
    constrain_queens(List,[]),
    labeling_ff(List),
    write(List).

make_list(0,[]):-!.
make_list(N,[_|Rest]):-
    N1 is N-1,
    make_list(N1,Rest).

constrain_queens([],Left).
constrain_queens([Q|Qs],Left):-
    constrain_queen(Q,Left,Qs),
    constrain_queens(Qs,[Q|Left]).
    
% delay the constraint until Q is instantiated
delay constrain_queen(Q,Left,Qs):-var(Q) : {ins(Q)}.
constrain_queen(Q,Left,Right):-true :
    exclude_positions(Q,1,Left),
    exclude_positions(Q,1,Right).

exclude_positions(Q0,N,[]).
exclude_positions(Q0,N,[Q|Qs]):-
    R1 is Q0-N,
    R2 is Q0+N,
    domain_set_false(Q,Q0), % not in the same row
    domain_set_false(Q,R1), % not in the same diagonal
    domain_set_false(Q,R2), % not in the same diagonal
    N1 is N+1,
    exclude_positions(Q0,N1,Qs).

⌨️ 快捷键说明

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