8 Queens Chess Problem
#1
The 8 queens chess problem is a math problem where 8 queens are placed on a chess board
such that no queen attacks another horizontally, vertically, or diagnollly.

Attached is a file with 3 QB64 programs which solve the queens problems for 8, 9, and 10 queens:

Erik.


Attached Files .zip   QUEENS.ZIP (Size: 4.19 KB / Downloads: 5)
dndbbs project:

Links to my MUD: (strictly 16-bit); AKA XP:

Dndbbs executables
http://www.filegate.net/pdn/pdnbasic/dnd50a1e.zip

Dndbbs source
http://www.filegate.net/pdn/pdnbasic/dnd50a1s.zip

Dndbbs upgrade
http://www.filegate.net/pdn/pdnbasic/dnd50a1u.zip

DNDDOOR - https://bit.ly/EriksDNDDoor DUNGEON - https://bit.ly/EriksDungeon
Interpreter - https://bit.ly/EriksSICK Hex Editor - https://bit.ly/EriksHexEditor Utilities - https://bit.ly/EriksUtils
QB45 files: - https://bit.ly/EriksQB45 QB64shell - https://bit.ly/QB64shell Some old QB64 versions: - https://bit.ly/OldQB64
Reply
#2
A variant to this problem.....what is the fewest number of queens that can be placed on an 8x8 board that attacks every square?
I like to program in BASIC
With code that is simple and slick
I learnt it in school
And it is still cool
So it is my number one pick
Reply
#3
This code solves any specified queens chess problem:

Code:
DEFINT A-Z
DIM SHARED QUEENS AS INTEGER
PRINT "# of queens";: INPUT QUEENS
IF QUEENS = 0 THEN END
PRINT "Calculating"; QUEENS; "queens problem:"
DIM SHARED arrayqcol(QUEENS) AS _BYTE ' columns of queens
DIM SHARED nsolutions AS DOUBLE
t! = TIMER
CALL dorow(1) ' start with row 1
PRINT STR$(nsolutions) + " solutions."
PRINT TIMER - t!; "seconds."
END
SUB dorow (irow) ' starts with row irow
    FOR icol = 1 TO QUEENS
        FOR iqueen = 1 TO irow - 1 ' check for conflict with previous queens
            IF arrayqcol(iqueen) = icol THEN GOTO continue1 ' same column?
            ' iqueen is also row of queen
            IF iqueen + arrayqcol(iqueen) = irow + icol THEN GOTO continue1 ' right diagonal?
            IF iqueen - arrayqcol(iqueen) = irow - icol THEN GOTO continue1 ' left diagonal?
        NEXT iqueen
        ' at this point we can add a queen
        arrayqcol(irow) = icol ' add to array
        IF irow = QUEENS THEN ' solution?
            nsolutions = nsolutions + 1
        ELSE
            CALL dorow(irow + 1) ' recursive call to next row
        END IF
        continue1:
    NEXT icol
END SUB
dndbbs project:

Links to my MUD: (strictly 16-bit); AKA XP:

Dndbbs executables
http://www.filegate.net/pdn/pdnbasic/dnd50a1e.zip

Dndbbs source
http://www.filegate.net/pdn/pdnbasic/dnd50a1s.zip

Dndbbs upgrade
http://www.filegate.net/pdn/pdnbasic/dnd50a1u.zip

DNDDOOR - https://bit.ly/EriksDNDDoor DUNGEON - https://bit.ly/EriksDungeon
Interpreter - https://bit.ly/EriksSICK Hex Editor - https://bit.ly/EriksHexEditor Utilities - https://bit.ly/EriksUtils
QB45 files: - https://bit.ly/EriksQB45 QB64shell - https://bit.ly/QB64shell Some old QB64 versions: - https://bit.ly/OldQB64
Reply
#4
Calculate queens in a loop:

Code:
REM code from dcromley 11/16/2017
DEFINT A-Z
REM $DYNAMIC
DIM SHARED QUEENS AS INTEGER
DIM SHARED nsolutions AS DOUBLE
DIM SHARED arrayqcol(1) AS _BYTE ' columns of queens
PRINT "MIN";: INPUT MIN
PRINT "MAX";: INPUT MAX
IF MIN = 0 THEN MIN = 8
IF MAX = 0 THEN MAX = 12
PRINT "Calculating boards from"; MIN; "to"; MAX; ".."
FOR QUEENS = MIN TO MAX
    PRINT "Calculating"; QUEENS; "queens:"
    nsolutions = 0
    REDIM SHARED arrayqcol(QUEENS) AS _BYTE
    t! = TIMER
    CALL dorow(1) ' start with row 1
    PRINT STR$(nsolutions) + " solutions,";
    PRINT TIMER - t!; "seconds."
NEXT
END

SUB dorow (irow) ' starts with row irow
    FOR icol = 1 TO QUEENS
        FOR iqueen = 1 TO irow - 1 ' check for conflict with previous queens
            IF arrayqcol(iqueen) = icol THEN GOTO continue1 ' same column?
            ' iqueen is also row of queen
            IF iqueen + arrayqcol(iqueen) = irow + icol THEN GOTO continue1 ' right diagonal?
            IF iqueen - arrayqcol(iqueen) = irow - icol THEN GOTO continue1 ' left diagonal?
        NEXT iqueen
        ' at this point we can add a queen
        arrayqcol(irow) = icol ' add to array
        IF irow = QUEENS THEN ' solution?
            nsolutions = nsolutions + 1
        ELSE
            CALL dorow(irow + 1) ' recursive call to next row
        END IF
        continue1:
    NEXT icol
END SUB
dndbbs project:

Links to my MUD: (strictly 16-bit); AKA XP:

Dndbbs executables
http://www.filegate.net/pdn/pdnbasic/dnd50a1e.zip

Dndbbs source
http://www.filegate.net/pdn/pdnbasic/dnd50a1s.zip

Dndbbs upgrade
http://www.filegate.net/pdn/pdnbasic/dnd50a1u.zip

DNDDOOR - https://bit.ly/EriksDNDDoor DUNGEON - https://bit.ly/EriksDungeon
Interpreter - https://bit.ly/EriksSICK Hex Editor - https://bit.ly/EriksHexEditor Utilities - https://bit.ly/EriksUtils
QB45 files: - https://bit.ly/EriksQB45 QB64shell - https://bit.ly/QB64shell Some old QB64 versions: - https://bit.ly/OldQB64
Reply
#5
Nice work, Erik!
http://rosettacode.org/wiki/N-queens_problem#QB64
I like to program in BASIC
With code that is simple and slick
I learnt it in school
And it is still cool
So it is my number one pick
Reply