# Question:queen problem by backtracking

## Question:queen problem by backtracking

Maple

my programme seems to be giving me all permutations on a nXn board been trying to find where i went wrong but its took me days and still cant locate ... i want this procedure to give me the n queens on a nxn board that are non attacking. please help

.place:=proc(z)
global L;
local i,k;
k:=z;
L:= Array(1..k);
for i from 1 to k do
if L(i)=L(k) # two in the same column
or abs(L(i)-L(k)) = abs(i-k) # in the same diagonal
then
return (false);
fi:
od:
return (true);
end proc:

queens:=proc(n)
local k,p,L;
p:=n;
L:=Array(1..n);
L(1):=0; # L(k) is the current column
k:=1; # k is the current row
while k > 0 do
L(k):= L(k)+1; # move to the next column
while L(k) <= n and place(k) do # can this queen be placed
L(k):= L(k) + 1;
od:
if L(k)<=n # a position is found
then if k = n # the solution has been found
then print (L);
else
k:=k+1;
L(k):=0;
fi:
else
k:= k-1; # backtrack
fi:
od:
end proc:

queens(5);

﻿