Solved

# 8-queens solution in PROLOG explaination needed (I am a beginer!!)

Posted on 2005-05-07
1,129 Views
generate(N, N, [N]).
generate(N, M, [N | Rest]) :-
N < M,
N1 is N + 1,
generate(N1, M, Rest).
solution(N, Ys):-
generate(1, N, RFvals),
Bot is 1 - N, Top is N - 1,
generate(Bot, Top, Uvals),
TwiceN is N + N,
generate(2, TwiceN, Vvals),
solution(Ys,RFvals, RFvals, Uvals, Vvals).
solution([], [], _, _, _).
solution([Y | Ylist], [X | Xsleft], Ys, Us, Vs) :-
insert(Y, Ysleft, Ys),
U is X - Y,
insert(U, Usleft, Us),
V is X + Y,
insert(V, Vsleft, Vs),
solution(Ylist, Xsleft, Ysleft, Usleft, Vsleft).

insert(X, L, [X | L]).
insert(X, [H | T], [H | XT]) :-
insert(X, T, XT).

Hi experts, I would like you to help me understanding the solution above written in PROLOG. Please provide me with a detailed explaination as soon as possible.
0
Question by:Al-Qershi

LVL 45

Accepted Solution

This is the recursive solution to 8 queens problem. In one line - take the list of board layouts from solutions to the "n-1 Queens Problem" and for each board, try adding a queen to each of the rows in the n-th column. If the new queen is "safe", add this board to the set of solutions to the "n Queens Problem".

Detailed explanations/diagrams can be found here
http://www.delphiforfun.org/Programs/EightQueensPlus.htm
http://www.cs.auc.dk/~normark/eciu-recursion/html/recit-note-8q-solution.html
http://www.tvcc.cc/staff/fuller/cs260/week5/week5.html
0

LVL 45

Expert Comment

0

## Featured Post

### Suggested Solutions

bigDiff challenge 17 60
isEverywhere  challenge 19 49
zeroMAx challenge 20 66
scoresSpecial  challenge 13 35
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.