if this was just about the code, theres a very simple solution that i could port right now. it finds one of the 92 solutions.
checking the solution isnt the difficult part. finding the valid solutions without wasting lots of time brute forcing them is ideally part of it.
i will be happy if i write it so it finds more than one solution, fast enough that i can come back later and its done. 2 ^ (8*8) is over 18 billion-billion, or more than a (short scale) quintillion.
- Code: Select all
# an approach to the 8 queens problem in fig
# public domain
# the idea is to set up the arrays of strings, as they are here:
# to get an array of say, row 8... now = r : mid 8 1 : split now " "
# that sets now to the python array ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8']
# so if you place a queen on that row, you can:
# * iterate through the other row items:
# * string find each of those items in columns and diags
# * remove those diags and columns and row items from board array "a"
# # technically, a is a string.
# now split a " " # now its an array named "now"
# a now # if you want to copy the array back to a, this did
# now join now " " # now its a string copied to now
# a now # now its a string copied to a (weve copied string to string a)
# #### how to string replace a substring:
# now = split a "a7" join now "x" # replace all instances of a7 in string a with "x" in string now
# a now # copy string now to string a
r "" arr times 8
r arrset 1 "a8 b8 c8 d8 e8 f8 g8 h8"
r arrset 2 "a7 b7 c7 d7 e7 f7 g7 h7"
r arrset 3 "a6 b6 c6 d6 e6 f6 g6 h6"
r arrset 4 "a5 b5 c5 d5 e5 f5 g5 h5"
r arrset 5 "a4 b4 c4 d4 e4 f4 g4 h4"
r arrset 6 "a3 b3 c3 d3 e3 f3 g3 h3"
r arrset 7 "a2 b2 c2 d2 e2 f2 g2 h2"
r arrset 8 "a1 b1 c1 d1 e1 f1 g1 h1"
c "" arr times 8
c arrset 1 "a8 a7 a6 a5 a4 a3 a2 a1"
c arrset 2 "b8 b7 b6 b5 b4 b3 b2 b1"
c arrset 3 "c8 c7 c6 c5 c4 c3 c2 c1"
c arrset 4 "d8 d7 d6 d5 d4 d3 d2 d1"
c arrset 5 "e8 e7 e6 e5 e4 e3 e2 e1"
c arrset 6 "f8 f7 f6 f5 f4 f3 f2 f1"
c arrset 7 "g8 g7 g6 g5 g4 g3 g2 g1"
c arrset 8 "h8 h7 h6 h5 h4 h3 h2 h1"
d "" arr times 30
d arrset 1 "a1"
d arrset 2 "a2 b1"
d arrset 3 "a3 b2 c1"
d arrset 4 "a4 b3 c2 d1"
d arrset 5 "a5 b4 c3 d2 e1"
d arrset 6 "a6 b5 c4 d3 e2 f1"
d arrset 7 "a7 b6 c5 d4 e3 f2 g1"
d arrset 8 "a8 b7 c6 d5 e4 f3 g2 h1"
d arrset 9 "b8 c7 d6 e5 f4 g3 h2"
d arrset 10 "c8 d7 e6 f5 g4 h3"
d arrset 11 "d8 e7 f6 g5 h4"
d arrset 12 "e8 f7 g6 h5"
d arrset 13 "f8 g7 h6"
d arrset 14 "g8 h7"
d arrset 15 "h8"
d arrset 16 "a8"
d arrset 17 "a7 b8"
d arrset 18 "a6 b7 c8"
d arrset 19 "a5 b6 c7 d8"
d arrset 20 "a4 b5 c6 d7 e8"
d arrset 21 "a3 b4 c5 d6 e7 f8"
d arrset 22 "a2 b3 c4 d5 e6 f7 g8"
d arrset 23 "a1 b2 c3 d4 e5 f6 g7 h8"
d arrset 24 "b1 c2 d3 e4 f5 g6 h7"
d arrset 25 "c1 d2 e3 f4 g5 h6"
d arrset 26 "d1 e2 f3 g4 h5"
d arrset 27 "e1 f2 g3 h4"
d arrset 28 "f1 g2 h3"
d arrset 29 "g1 h2"
d arrset 30 "h1"
a = join r " "
# abcdefgh
# 8
# 7
# 6
# 5
# 4
# 3
# 2
# 1
# r 8 rows, 1 row per space-delim string
# c 8 cols, 1 col per space-delim string
# a 64 positions, space-delim single string
# d 1-15 diags, 1 diag per space-delim string
# d 16-30 diags, 1 diag per space-delim string
# to get row 8 as a space-delimited string, convert to array of 8 position names:
# now = r : mid 8 1 : split now " " # ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8']
copy a split copy " "
#function aset ar po
function disp ar
now "" print
c 0
for y 1 8 1
for x 1 8 1
inc c plus 1 swap inc c
now ar mid c 1 plus " " left 3 prints
next
now "" print
next
fig
function xrows ar pose
now return ar
fig
function xcols ar pose
now return ar
fig
function xdgs ar pose
now return ar
fig
for pose 1 4 1
ag arrget copy pose
ifequal ag "x"
pass
else
ifequal ag "q"
pass
else
copy arrset pose "q"
copy xrows copy pose
copy xcols copy pose
copy xdgs copy pose
fig
fig
now disp copy
next