How to resolve the algorithm N-queens problem step by step in the Maple programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm N-queens problem step by step in the Maple programming language

Table of Contents

Problem Statement

Solve the eight queens puzzle.

You can extend the problem to solve the puzzle with a board of size   NxN. For the number of solutions for small values of   N,   see   OEIS: A000170.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm N-queens problem step by step in the Maple programming language

Source code in the maple programming language

queens:=proc(n)
  local a,u,v,m,aux;
  a:=[$1..n];
  u:=[true$2*n-1];
  v:=[true$2*n-1];
  m:=[];
  aux:=proc(i)
    local j,k,p,q;
    if i>n then
      m:=[op(m),copy(a)];
    else
      for j from i to n do
        k:=a[j];
        p:=i-k+n;
        q:=i+k-1;
        if u[p] and v[q] then
          u[p]:=false;
          v[q]:=false;
          a[j]:=a[i];
          a[i]:=k;
          aux(i+1);
          u[p]:=true;
          v[q]:=true;
          a[i]:=a[j];
          a[j]:=k;
        fi;
      od;
    fi;
  end;
  aux(1);
  m
end:          

for a in queens(8) do printf("%a\n",a) od;

  

You may also check:How to resolve the algorithm Primality by trial division step by step in the Sidef programming language
You may also check:How to resolve the algorithm Yin and yang step by step in the PL/I programming language
You may also check:How to resolve the algorithm Show the epoch step by step in the Tcl programming language
You may also check:How to resolve the algorithm Department numbers step by step in the Java programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the VBA programming language