Created
May 30, 2018 23:44
-
-
Save greggirwin/f8564168f6cc5a6a0bb4904344b24c5d to your computer and use it in GitHub Desktop.
The N-Queens problem, with a different approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REBOL [ | |
Title: "n-queens" | |
Author: "Gregg Irwin" | |
Comment: { | |
I just cooked this up in my head. It could fail miserably in some cases. | |
The special N // 6 = 2 case isn't something I discovered, but the | |
"pathing" algorithm used is mine. | |
} | |
] | |
n-queens: func [ | |
"Returns a string describing the solution for the N Queens problem for N." | |
n [integer!] "The size of the board on which to place the queens" | |
][ | |
if n < 4 [return "Sorry, there is no solution."] | |
if all [even? n 2 <> remainder n 6] [ return rejoin [ { | |
1) Start at R1C2, make knights moves (right 2 down 1) until you can't | |
make any more moves without going off the board. | |
2) Start at R} (n) "C" (n - 1) { , make knights moves (left 2 up 1) until you can't | |
make any more moves without going off the board. This is a mirror | |
image operation of step 1. | |
} | |
] | |
] | |
if all [even? n 2 = remainder n 6] [ return rejoin [ { | |
1) Start at R1C} (n / 2) {, make knights moves (right 2 down 1) until you can't | |
make any more moves without going off the board. Now, you're going | |
to wrap back around to the left side of the board, dropping down to | |
the next row from where you left off. You'll start in column } | |
either even? n / 2 [2][1] {. | |
You can also look at is as just continuing the knights move, but | |
wrapping around the edge of the board. | |
Keep going until you've placed } (n / 2) { queens total. | |
2) Like before, do a mirror image operation of step 1, starting from | |
R} (n) {C} (n / 2 + 1) { and working your way up and to the left. | |
} | |
] | |
] | |
if all [odd? n 2 <> remainder (n - 1) 6] [ return rejoin [ { | |
1) Start at R1C2, make knights moves (right 2 down 1) until you can't | |
make any more moves without going off the board. | |
2) Start at R} (n) {C} (n) {, make knights moves (left 2 up 1) until you can't | |
make any more moves without going off the board. This is a mirror | |
image operation of step 1, but starting in the lower right corner. | |
} | |
] | |
] | |
if all [odd? n 2 = remainder (n - 1) 6] [ return rejoin [ { | |
1) Start at R1C2, make knights moves (right 2 down 1) until you can't | |
make any more moves without going off the board. | |
2) Start at R} (n - 1) {C} (n) {, make knights moves (left 2 up 1) until you get | |
to column 1. Place the queen for column 1 at R} (n) {C1. This is a mirror | |
image operation of step 1, but starting one cell up from the lower | |
right corner, and with a final "break move". | |
} | |
] | |
] | |
] | |
print "solution for 3:" | |
print n-queens 3 | |
print "solution for 5:" | |
print n-queens 5 | |
print "solution for 6:" | |
print n-queens 6 | |
print "solution for 8:" | |
print n-queens 8 | |
print "solution for 14:" | |
print n-queens 14 | |
print "solution for 9:" | |
print n-queens 9 | |
halt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment