5come5帮你背单词 [ buddhist /'budist/ n. 佛教徒
]
求助---一个N-皇后求解问题
inlist(1,L,[1|L]). inlist(X,L1,L2):- X>1, Y is X-1, inlist(Y,[X|L1],L2).
queens(N,Solutions):- inlist(N,[],L), solve([],Solutons,L,L).
solve(Input,Output,[Row|R],Columns):- choose(Col,Columns,C), Up is Row+Col, Down is Row-Col, safe(Up,Down,Input), solve([square(Col,Up,Down)|Input],Output,R,C). solve(L,L,[],[]).
choose(X,[X|Y],Y). choose(X,[H|T1],[H|T2]):- choose(X,T1,T2).
safe(U1,D1,[square(_,U,D)|Rest]):- U1\=U,D1\=D, safe(U1,D1,Rest). safe(_,_,[]).
n_queens:- write("The size of the chessboar N="), read(N), queens(N,Solutions), print_board(N,Solutions), fail.
print_board(N,[square(X,_,_)|L]):- nl, Y is X-1, write_dots(Y), write(Q), W is N-X, write_dots(W), print_board(N,L).
write_dots(X):- X>0, write("."), Y is X-1, write_dots(Y).
write_dots(0).
n_queens. The size of the chessboar N=2 THE$126 Syntax error in parser No solutions Reconsulted from: C:\Documents and Settings\Administrator\My Documents\Visual Prolog 7.0 Examples\pie\EXE\FILE6.PRO n_queens. The size of the chessboar N=Trace is On 3 Trace: >> CALL: queens(3,SOLUTIONS$127) Trace: >> CALL: inlist(3,[],L$128) Trace: >> CALL: 3 > 1 Trace: >> RETURN: 3 > 1 Trace: >> CALL: Y$129 is 3 - 1 Trace: >> RETURN: 2 is 3 - 1 Trace: >> CALL: inlist(2,[3|[]],L$128) Trace: >> CALL: 2 > 1 Trace: >> RETURN: 2 > 1 Trace: >> CALL: Y$130 is 2 - 1 Trace: >> RETURN: 1 is 2 - 1 Trace: >> CALL: inlist(1,[2|[3|[]]],L$128) Trace: >> RETURN: inlist(1,[2|[3|[]]],[1|[2|[3|[]]]]) Trace: >> RETURN: inlist(2,[3|[]],[1|[2|[3|[]]]]) Trace: >> RETURN: inlist(3,[],[1|[2|[3|[]]]]) Trace: >> CALL: solve([],SOLUTONS$131,[1|[2|[3|[]]]],[1|[2|[3|[]]]]) Trace: >> CALL: choose(COL$132,[1|[2|[3|[]]]],C$133) Trace: >> RETURN: choose(1,[1|[2|[3|[]]]],[2|[3|[]]]) Trace: >> CALL: UP$134 is 1 + 1 Trace: >> RETURN: 2 is 1 + 1 Trace: >> CALL: DOWN$135 is 1 - 1 Trace: >> RETURN: 0 is 1 - 1 Trace: >> CALL: safe(2,0,[]) Trace: >> RETURN: safe(2,0,[]) Trace: >> CALL: solve([square(1,2,0)|[]],SOLUTONS$131,[2|[3|[]]],[2|[3|[]]]) Trace: >> CALL: choose(COL$136,[2|[3|[]]],C$137) Trace: >> RETURN: choose(2,[2|[3|[]]],[3|[]]) Trace: >> CALL: UP$138 is 2 + 2 Trace: >> RETURN: 4 is 2 + 2 Trace: >> CALL: DOWN$139 is 2 - 2 Trace: >> RETURN: 0 is 2 - 2 Trace: >> CALL: safe(4,0,[square(1,2,0)|[]]) Trace: >> CALL: 4 \= 2 Trace: >> RETURN: 4 \= 2 Trace: >> CALL: 0 \= 0 Trace: >> FAIL: 0 \= 0 Trace: >> REDO: 4 \= 2 Trace: >> FAIL: 4 \= 2 Trace: >> FAIL: safe(4,0,[square(1,2,0)|[]]) Trace: >> REDO: 0 is 2 - 2 Trace: >> FAIL: DOWN$139 is 2 - 2 Trace: >> REDO: 4 is 2 + 2 Trace: >> FAIL: UP$138 is 2 + 2 Trace: >> REDO: choose(2,[2|[3|[]]],[3|[]]) Trace: >> CALL: choose(COL$136,[3|[]],T2$140) Trace: >> RETURN: choose(3,[3|[]],[]) Trace: >> RETURN: choose(3,[2|[3|[]]],[2|[]]) Trace: >> CALL: UP$138 is 2 + 3 Trace: >> RETURN: 5 is 2 + 3 Trace: >> CALL: DOWN$139 is 2 - 3 Trace: >> RETURN: -1 is 2 - 3 Trace: >> CALL: safe(5,-1,[square(1,2,0)|[]]) Trace: >> CALL: 5 \= 2 Trace: >> RETURN: 5 \= 2 Trace: >> CALL: -1 \= 0 Trace: >> RETURN: -1 \= 0 Trace: >> CALL: safe(5,-1,[]) Trace: >> RETURN: safe(5,-1,[]) Trace: >> RETURN: safe(5,-1,[square(1,2,0)|[]]) Trace: >> CALL: solve([square(3,5,-1)|[square(1,2,0)|[]]],SOLUTONS$131,[3|[]],[2|[]]) Trace: >> CALL: choose(COL$141,[2|[]],C$142) Trace: >> RETURN: choose(2,[2|[]],[]) Trace: >> CALL: UP$143 is 3 + 2 Trace: >> RETURN: 5 is 3 + 2 Trace: >> CALL: DOWN$144 is 3 - 2 Trace: >> RETURN: 1 is 3 - 2 Trace: >> CALL: safe(5,1,[square(3,5,-1)|[square(1,2,0)|[]]]) Trace: >> CALL: 5 \= 5 Trace: >> FAIL: 5 \= 5 Trace: >> FAIL: safe(5,1,[square(3,5,-1)|[square(1,2,0)|[]]]) Trace: >> REDO: 1 is 3 - 2 Trace: >> FAIL: DOWN$144 is 3 - 2 Trace: >> REDO: 5 is 3 + 2 Trace: >> FAIL: UP$143 is 3 + 2 Trace: >> REDO: choose(2,[2|[]],[]) Trace: >> CALL: choose(COL$141,[],T2$145) Trace: >> FAIL: choose(COL$141,[],T2$145) Trace: >> FAIL: choose(COL$141,[2|[]],C$142) Trace: >> FAIL: solve([square(3,5,-1)|[square(1,2,0)|[]]],SOLUTONS$131,[3|[]],[2|[]]) Trace: >> REDO: safe(5,-1,[square(1,2,0)|[]]) Trace: >> REDO: safe(5,-1,[]) Trace: >> FAIL: safe(5,-1,[]) Trace: >> REDO: -1 \= 0 Trace: >> FAIL: -1 \= 0 Trace: >> REDO: 5 \= 2 Trace: >> FAIL: 5 \= 2 Trace: >> FAIL: safe(5,-1,[square(1,2,0)|[]]) Trace: >> REDO: -1 is 2 - 3 Trace: >> FAIL: DOWN$139 is 2 - 3 Trace: >> REDO: 5 is 2 + 3 Trace: >> FAIL: UP$138 is 2 + 3 Trace: >> REDO: choose(3,[2|[3|[]]],[2|[]]) Trace: >> REDO: choose(3,[3|[]],[]) Trace: >> CALL: choose(COL$136,[],T2$146) Trace: >> FAIL: choose(COL$136,[],T2$146) Trace: >> FAIL: choose(COL$136,[3|[]],T2$140) Trace: >> FAIL: choose(COL$136,[2|[3|[]]],C$137) Trace: >> FAIL: solve([square(1,2,0)|[]],SOLUTONS$131,[2|[3|[]]],[2|[3|[]]]) Trace: >> REDO: safe(2,0,[]) Trace: >> FAIL: safe(2,0,[]) Trace: >> REDO: 0 is 1 - 1 Trace: >> FAIL: DOWN$135 is 1 - 1 Trace: >> REDO: 2 is 1 + 1 Trace: >> FAIL: UP$134 is 1 + 1 Trace: >> REDO: choose(1,[1|[2|[3|[]]]],[2|[3|[]]]) Trace: >> CALL: choose(COL$132,[2|[3|[]]],T2$147) Trace: >> RETURN: choose(2,[2|[3|[]]],[3|[]]) Trace: >> RETURN: choose(2,[1|[2|[3|[]]]],[1|[3|[]]]) Trace: >> CALL: UP$134 is 1 + 2 Trace: >> RETURN: 3 is 1 + 2 Trace: >> CALL: DOWN$135 is 1 - 2 Trace: >> RETURN: -1 is 1 - 2 Trace: >> CALL: safe(3,-1,[]) Trace: >> RETURN: safe(3,-1,[]) Trace: >> CALL: solve([square(2,3,-1)|[]],SOLUTONS$131,[2|[3|[]]],[1|[3|[]]]) Trace: >> CALL: choose(COL$148,[1|[3|[]]],C$149) Trace: >> RETURN: choose(1,[1|[3|[]]],[3|[]]) Trace: >> CALL: UP$150 is 2 + 1 Trace: >> RETURN: 3 is 2 + 1 Trace: >> CALL: DOWN$151 is 2 - 1 Trace: >> RETURN: 1 is 2 - 1 Trace: >> CALL: safe(3,1,[square(2,3,-1)|[]]) Trace: >> CALL: 3 \= 3 Trace: >> FAIL: 3 \= 3 Trace: >> FAIL: safe(3,1,[square(2,3,-1)|[]]) Trace: >> REDO: 1 is 2 - 1 Trace: >> FAIL: DOWN$151 is 2 - 1 Trace: >> REDO: 3 is 2 + 1 Trace: >> FAIL: UP$150 is 2 + 1 Trace: >> REDO: choose(1,[1|[3|[]]],[3|[]]) Trace: >> CALL: choose(COL$148,[3|[]],T2$152) Trace: >> RETURN: choose(3,[3|[]],[]) Trace: >> RETURN: choose(3,[1|[3|[]]],[1|[]]) Trace: >> CALL: UP$150 is 2 + 3 Trace: >> RETURN: 5 is 2 + 3 Trace: >> CALL: DOWN$151 is 2 - 3 Trace: >> RETURN: -1 is 2 - 3 Trace: >> CALL: safe(5,-1,[square(2,3,-1)|[]]) Trace: >> CALL: 5 \= 3 Trace: >> RETURN: 5 \= 3 Trace: >> CALL: -1 \= -1 Trace: >> FAIL: -1 \= -1 Trace: >> REDO: 5 \= 3 Trace: >> FAIL: 5 \= 3 Trace: >> FAIL: safe(5,-1,[square(2,3,-1)|[]]) Trace: >> REDO: -1 is 2 - 3 Trace: >> FAIL: DOWN$151 is 2 - 3 Trace: >> REDO: 5 is 2 + 3 Trace: >> FAIL: UP$150 is 2 + 3 Trace: >> REDO: choose(3,[1|[3|[]]],[1|[]]) Trace: >> REDO: choose(3,[3|[]],[]) Trace: >> CALL: choose(COL$148,[],T2$153) Trace: >> FAIL: choose(COL$148,[],T2$153) Trace: >> FAIL: choose(COL$148,[3|[]],T2$152) Trace: >> FAIL: choose(COL$148,[1|[3|[]]],C$149) Trace: >> FAIL: solve([square(2,3,-1)|[]],SOLUTONS$131,[2|[3|[]]],[1|[3|[]]]) Trace: >> REDO: safe(3,-1,[]) Trace: >> FAIL: safe(3,-1,[]) Trace: >> REDO: -1 is 1 - 2 Trace: >> FAIL: DOWN$135 is 1 - 2 Trace: >> REDO: 3 is 1 + 2 Trace: >> FAIL: UP$134 is 1 + 2 Trace: >> REDO: choose(2,[1|[2|[3|[]]]],[1|[3|[]]]) Trace: >> REDO: choose(2,[2|[3|[]]],[3|[]]) Trace: >> CALL: choose(COL$132,[3|[]],T2$154) Trace: >> RETURN: choose(3,[3|[]],[]) Trace: >> RETURN: choose(3,[2|[3|[]]],[2|[]]) Trace: >> RETURN: choose(3,[1|[2|[3|[]]]],[1|[2|[]]]) Trace: >> CALL: UP$134 is 1 + 3 Trace: >> RETURN: 4 is 1 + 3 Trace: >> CALL: DOWN$135 is 1 - 3 Trace: >> RETURN: -2 is 1 - 3 Trace: >> CALL: safe(4,-2,[]) Trace: >> RETURN: safe(4,-2,[]) Trace: >> CALL: solve([square(3,4,-2)|[]],SOLUTONS$131,[2|[3|[]]],[1|[2|[]]]) Trace: >> CALL: choose(COL$155,[1|[2|[]]],C$156) Trace: >> RETURN: choose(1,[1|[2|[]]],[2|[]]) Trace: >> CALL: UP$157 is 2 + 1 Trace: >> RETURN: 3 is 2 + 1 Trace: >> CALL: DOWN$158 is 2 - 1 Trace: >> RETURN: 1 is 2 - 1 Trace: >> CALL: safe(3,1,[square(3,4,-2)|[]]) Trace: >> CALL: 3 \= 4 Trace: >> RETURN: 3 \= 4 Trace: >> CALL: 1 \= -2 Trace: >> RETURN: 1 \= -2 Trace: >> CALL: safe(3,1,[]) Trace: >> RETURN: safe(3,1,[]) Trace: >> RETURN: safe(3,1,[square(3,4,-2)|[]]) Trace: >> CALL: solve([square(1,3,1)|[square(3,4,-2)|[]]],SOLUTONS$131,[3|[]],[2|[]]) Trace: >> CALL: choose(COL$159,[2|[]],C$160) Trace: >> RETURN: choose(2,[2|[]],[]) Trace: >> CALL: UP$161 is 3 + 2 Trace: >> RETURN: 5 is 3 + 2 Trace: >> CALL: DOWN$162 is 3 - 2 Trace: >> RETURN: 1 is 3 - 2 Trace: >> CALL: safe(5,1,[square(1,3,1)|[square(3,4,-2)|[]]]) Trace: >> CALL: 5 \= 3 Trace: >> RETURN: 5 \= 3 Trace: >> CALL: 1 \= 1 Trace: >> FAIL: 1 \= 1 Trace: >> REDO: 5 \= 3 Trace: >> FAIL: 5 \= 3 Trace: >> FAIL: safe(5,1,[square(1,3,1)|[square(3,4,-2)|[]]]) Trace: >> REDO: 1 is 3 - 2 Trace: >> FAIL: DOWN$162 is 3 - 2 Trace: >> REDO: 5 is 3 + 2 Trace: >> FAIL: UP$161 is 3 + 2 Trace: >> REDO: choose(2,[2|[]],[]) Trace: >> CALL: choose(COL$159,[],T2$163) Trace: >> FAIL: choose(COL$159,[],T2$163) Trace: >> FAIL: choose(COL$159,[2|[]],C$160) Trace: >> FAIL: solve([square(1,3,1)|[square(3,4,-2)|[]]],SOLUTONS$131,[3|[]],[2|[]]) Trace: >> REDO: safe(3,1,[square(3,4,-2)|[]]) Trace: >> REDO: safe(3,1,[]) Trace: >> FAIL: safe(3,1,[]) Trace: >> REDO: 1 \= -2 Trace: >> FAIL: 1 \= -2 Trace: >> REDO: 3 \= 4 Trace: >> FAIL: 3 \= 4 Trace: >> FAIL: safe(3,1,[square(3,4,-2)|[]]) Trace: >> REDO: 1 is 2 - 1 Trace: >> FAIL: DOWN$158 is 2 - 1 Trace: >> REDO: 3 is 2 + 1 Trace: >> FAIL: UP$157 is 2 + 1 Trace: >> REDO: choose(1,[1|[2|[]]],[2|[]]) Trace: >> CALL: choose(COL$155,[2|[]],T2$164) Trace: >> RETURN: choose(2,[2|[]],[]) Trace: >> RETURN: choose(2,[1|[2|[]]],[1|[]]) Trace: >> CALL: UP$157 is 2 + 2 Trace: >> RETURN: 4 is 2 + 2 Trace: >> CALL: DOWN$158 is 2 - 2 Trace: >> RETURN: 0 is 2 - 2 Trace: >> CALL: safe(4,0,[square(3,4,-2)|[]]) Trace: >> CALL: 4 \= 4 Trace: >> FAIL: 4 \= 4 Trace: >> FAIL: safe(4,0,[square(3,4,-2)|[]]) Trace: >> REDO: 0 is 2 - 2 Trace: >> FAIL: DOWN$158 is 2 - 2 Trace: >> REDO: 4 is 2 + 2 Trace: >> FAIL: UP$157 is 2 + 2 Trace: >> REDO: choose(2,[1|[2|[]]],[1|[]]) Trace: >> REDO: choose(2,[2|[]],[]) Trace: >> CALL: choose(COL$155,[],T2$165) Trace: >> FAIL: choose(COL$155,[],T2$165) Trace: >> FAIL: choose(COL$155,[2|[]],T2$164) Trace: >> FAIL: choose(COL$155,[1|[2|[]]],C$156) Trace: >> FAIL: solve([square(3,4,-2)|[]],SOLUTONS$131,[2|[3|[]]],[1|[2|[]]]) Trace: >> REDO: safe(4,-2,[]) Trace: >> FAIL: safe(4,-2,[]) Trace: >> REDO: -2 is 1 - 3 Trace: >> FAIL: DOWN$135 is 1 - 3 Trace: >> REDO: 4 is 1 + 3 Trace: >> FAIL: UP$134 is 1 + 3 Trace: >> REDO: choose(3,[1|[2|[3|[]]]],[1|[2|[]]]) Trace: >> REDO: choose(3,[2|[3|[]]],[2|[]]) Trace: >> REDO: choose(3,[3|[]],[]) Trace: >> CALL: choose(COL$132,[],T2$166) Trace: >> FAIL: choose(COL$132,[],T2$166) Trace: >> FAIL: choose(COL$132,[3|[]],T2$154) Trace: >> FAIL: choose(COL$132,[2|[3|[]]],T2$147) Trace: >> FAIL: choose(COL$132,[1|[2|[3|[]]]],C$133) Trace: >> FAIL: solve([],SOLUTONS$131,[1|[2|[3|[]]]],[1|[2|[3|[]]]]) Trace: >> REDO: inlist(3,[],[1|[2|[3|[]]]]) Trace: >> REDO: inlist(2,[3|[]],[1|[2|[3|[]]]]) Trace: >> REDO: inlist(1,[2|[3|[]]],[1|[2|[3|[]]]]) Trace: >> CALL: 1 > 1 Trace: >> FAIL: 1 > 1 Trace: >> FAIL: inlist(1,[2|[3|[]]],L$128) Trace: >> REDO: 1 is 2 - 1 Trace: >> FAIL: Y$130 is 2 - 1 Trace: >> REDO: 2 > 1 Trace: >> FAIL: 2 > 1 Trace: >> FAIL: inlist(2,[3|[]],L$128) Trace: >> REDO: 2 is 3 - 1 Trace: >> FAIL: Y$129 is 3 - 1 Trace: >> REDO: 3 > 1 Trace: >> FAIL: 3 > 1 Trace: >> FAIL: inlist(3,[],L$128) Trace: >> FAIL: queens(3,SOLUTIONS$
|