我正在解决 prolog 中的 8 个皇后问题。我是序言的初学者。我想打印解决方案列表,但我的查询每次都失败......任何人都可以帮助我???
package family;
import java.util.*;
import jpl.*;
import jpl.Query;
/**
*
*/
public class family {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Query q1 = new Query("consult", new Term[] {new Atom("check.pl")});
System.out.println((q1.hasSolution() ? "succeeded" : "failed"));
java.util.Hashtable solution;
Variable X = new Variable();
Query q4 = new Query(new Compound("solution", new Term[] { new jpl.Integer(8), X}));
System.out.println((q4.hasSolution() ? "succeeded" : "failed"));
while ( q4.hasMoreSolutions() )
{
solution = q4.nextSolution();
System.out.println( "X = " + solution.get("X"));
}
}
}
这是我的序言代码
solution( Ylist) :-
sol( Ylist, [1,2,3,4,5,6,7,8], [1,2,3,4,5,6,7,8],
[-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7],
[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] ).
%sol( [], [], Dy, Du, Dv). sol( [Y | Ylist], [X | Dx1], Dy, Du, Dv) :-
del( Y, Dy, Dy1), U is X-Y, del( U, Du, Du1), V is X+Y,
del( V, Dv, Dv1), sol( Ylist, Dx1, Dy1, Du1, Dv1).
del( Item, [Item | List], List). del( Item, [First | List], [First | List1] ) :-
del( Item, List, List1).
gen( N, N, [N]).
gen( N1, N2, [N1|List]) :-
N1 < N2, M is N1+1, gen(M, N2, List).
solution( N, S) :- gen(1, N, Dxy), Nu1 is 1-N, Nu2 is N-1,
gen(Nu1, Nu2, Du), Nv2 is N+N,
gen(2, Nv2, Dv), sol( S, Dxy, Dxy, Du, Dv).