From 8-Queens solution to more generic n-Queens solution in Prolog -


i studying prolog universitary exame , have problem following exercise.

i have following classic solution of 8-queens problem (and not problem me), modifying solution have create new solution more generic n-queens problem handle variable number of queens.

solution([]).  solution([x/y|others]) :- solution(others),                           member(y,[1,2,3,4,5,6,7,8]),                           noattack(x/y, others).  noattack(_,[]).  noattack(x/y, [x1/y1 | others]) :-                       y =\= y1,       % q e q1 sono su righe diverse                       % q e q1 sono su diagonali diverse:                       y1-y =\= x1-x,                       y1-y =\= x-x1,                       % q non attacca regine nella sottolista others:                       noattack( x/y, others).  % template delle soluzioni: c'è una regina su ogni colonna: template([1/y1,2/y2,3/y3,4/y4,5/y5,6/y6,7/y7,8/y8]). 

ok, program pretty simple: have list of queen have must not attack each other.

if list of queen empty there not possibility queen attack queen in list, empty list solution of problem (it base case of solution)

*if list of queen not empty can divide [x/y|others] x/y rappresent position on board of first queen in list *(the position rappresentend couple (x,y) x column , y line)

so, true list [x/y|others] solution of problem if following relations true:

  1. the sublist others solution (others don't contain queen attack other queen in list)

  2. y belongs belongs integer value between 1 , 8 (because have 8 line)

  3. the first queen of list don't attacck others queens in sublist others

then defined noattack relation specify when can true queen don't attack queen (this pretty simple: can't stay on same line, on same column, on same diagonal)

finally have solution template simplify life constraining x value value 1 8 (because know 2 queens can't stay on same columns...so every queen in solution stay on different column others queens)

so think biggest problem on line in specify number of columns:

member(y,[1,2,3,4,5,6,7,8]) 

and on line in define solution template:

template([1/y1,2/y2,3/y3,4/y4,5/y5,6/y6,7/y7,8/y8]). 

i have no idea how extend previous solution handle variable number of queens.

seems easy, passing around size:

solution(_, []). solution(n, [x/y|others]) :-     solution(n, others),     between(1, n, y),     noattack(x/y, others).  noattack(_,[]). noattack(x/y, [x1/y1 | others]) :-     y =\= y1,       % q e q1 sono su righe diverse     y1-y =\= x1-x,  % q e q1 sono su diagonali diverse     y1-y =\= x-x1,     noattack( x/y, others). % q non attacca regine nella sottolista others  % template delle soluzioni: c'è una regina su ogni colonna: template(n, l) :-     findall(i/_, between(1,n,i), l). 

test:

?- n=6, template(n, l), solution(n, l). n = 6, l = [1/5, 2/3, 3/1, 4/6, 5/4, 6/2] ; n = 6, l = [1/4, 2/1, 3/5, 4/2, 5/6, 6/3] ; n = 6, l = [1/3, 2/6, 3/2, 4/5, 5/1, 6/4] ; n = 6, l = [1/2, 2/4, 3/6, 4/1, 5/3, 6/5] ; false. 

(i should draw if it's ok...)


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -