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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm N-queens problem step by step in the PowerBASIC 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 PowerBASIC programming language

Source code in the powerbasic programming language

#COMPILE EXE
#DIM ALL

SUB aux(n AS INTEGER, i AS INTEGER, a() AS INTEGER, _
        u() AS INTEGER, v() AS INTEGER, m AS QUAD)

    LOCAL j, k, p, q AS INTEGER
    IF i > n THEN
        INCR m
        FOR k = 1 TO n : PRINT a(k); : NEXT : PRINT
    ELSE
        FOR j = i TO n
            k = a(j)
            p = i - k + n
            q = i + k - 1
            IF u(p) AND v(q) THEN
                u(p) = 0 : v(q) = 0
                a(j) = a(i) : a(i) = k
                CALL aux(n, i + 1, a(), u(), v(), m)
                u(p) = 1 : v(q) = 1
                a(i) = a(j) : a(j) = k
            END IF
        NEXT
    END IF
END SUB

FUNCTION PBMAIN () AS LONG
    LOCAL n, i AS INTEGER
    LOCAL m AS QUAD
    IF COMMAND$(1) <> "" THEN
        n = VAL(COMMAND$(1))
        REDIM a(1 TO n) AS INTEGER
        REDIM u(1 TO 2 * n - 1) AS INTEGER
        REDIM v(1 TO 2 * n - 1) AS INTEGER
        FOR i = 1 TO n
            a(i) = i
        NEXT
        FOR i = 1 TO 2 * n - 1
            u(i) = 1
            v(i) = 1
        NEXT
        m = 0
        CALL aux(n, 1, a(), u(), v(), m)
        PRINT m
    END IF
END FUNCTION

#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG
    LOCAL n, i, j, k, p, q AS INTEGER
    LOCAL m AS QUAD
    IF COMMAND$(1) <> "" THEN
        n = VAL(COMMAND$(1))
        REDIM a(1 TO n) AS INTEGER
        REDIM s(1 TO n) AS INTEGER
        REDIM u(1 TO 2 * n - 1) AS INTEGER
        REDIM v(1 TO 2 * n - 1) AS INTEGER
        FOR i = 1 TO n
            a(i) = i
        NEXT
        FOR i = 1 TO 2 * n - 1
            u(i) = 1
            v(i) = 1
        NEXT
        m = 0
        i = 1
      1 IF i > n THEN
            INCR m
            FOR k = 1 TO n : PRINT a(k); : NEXT : PRINT
            GOTO 4
        END IF
        j = i
      2 k = a(j)
        p = i - k + n
        q = i + k - 1
        IF u(p) AND v(q) THEN
            u(p) = 0 : v(q) = 0
            a(j) = a(i) : a(i) = k
            s(i) = j
            INCR i
            GOTO 1
        END IF
      3 INCR j : IF j <= n GOTO 2
      4 DECR i : IF i = 0 THEN PRINT m : EXIT FUNCTION
        j = s(i)
        k = a(i) : a(i) = a(j) : a(j) = k
        p = i - k + n
        q = i + k - 1
        u(p) = 1 : v(q) = 1
        GOTO 3
    END IF
END FUNCTION

  

You may also check:How to resolve the algorithm Formatted numeric output step by step in the REBOL programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Peaceful chess queen armies step by step in the C# programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the BASIC256 programming language
You may also check:How to resolve the algorithm Loops/While step by step in the blz programming language