• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1171
  • Last Modified:

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

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
Al-Qershi
Asked:
Al-Qershi
  • 2
1 Solution
 
sunnycoderCommented:
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
 
sunnycoderCommented:
Why the C grade?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now