2024-06-19 21:57:43 +00:00
|
|
|
%import textio
|
|
|
|
|
|
|
|
; Recursive N-Queens solver.
|
|
|
|
; The problem is: find all possible ways to place 8 Queen chess pieces on a chess board, so that none of them attacks any other.
|
|
|
|
; (this program prints all solutions without taking mirroring and flipping the chess board into account)
|
|
|
|
; Note: this program can be compiled for multiple target systems.
|
|
|
|
|
|
|
|
main {
|
|
|
|
const ubyte NUMQUEENS=8
|
|
|
|
ubyte[NUMQUEENS] board
|
|
|
|
|
2024-07-20 20:36:19 +00:00
|
|
|
sub could_place(ubyte row, ubyte col) -> bool {
|
|
|
|
if row==0
|
|
|
|
return true
|
|
|
|
ubyte i
|
2024-06-19 21:57:43 +00:00
|
|
|
for i in 0 to row-1 {
|
|
|
|
if board[i]==col or board[i]-i==col-row or board[i]+i==col+row
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
ubyte solution_count
|
|
|
|
sub print_solution() {
|
|
|
|
solution_count++
|
|
|
|
txt.home()
|
|
|
|
txt.print("found solution ")
|
|
|
|
txt.print_ub(solution_count)
|
|
|
|
txt.nl()
|
|
|
|
ubyte i
|
|
|
|
for i in 0 to NUMQUEENS-1 {
|
|
|
|
ubyte col = board[i]
|
|
|
|
txt.chrout(' ')
|
|
|
|
repeat col txt.chrout('.')
|
|
|
|
txt.chrout('q')
|
|
|
|
repeat NUMQUEENS-col-1 txt.chrout('.')
|
|
|
|
txt.nl()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub place_queen(ubyte row) {
|
|
|
|
if row == NUMQUEENS {
|
|
|
|
print_solution()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ubyte col
|
|
|
|
for col in 0 to NUMQUEENS-1 {
|
2024-07-20 20:36:19 +00:00
|
|
|
if could_place(row, col) {
|
2024-06-19 21:57:43 +00:00
|
|
|
board[row] = col
|
|
|
|
; we need to save the local variables row and col.
|
|
|
|
sys.push(row)
|
|
|
|
sys.push(col)
|
|
|
|
place_queen(row + 1)
|
|
|
|
; restore the local variables after the recursive call.
|
|
|
|
col = sys.pop()
|
|
|
|
row = sys.pop()
|
|
|
|
board[row] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
cbm.SETTIM(0,0,0)
|
|
|
|
txt.clear_screen()
|
|
|
|
place_queen(0)
|
|
|
|
txt.nl()
|
|
|
|
uword duration=100*cbm.RDTIM16()/6
|
|
|
|
txt.print_uw(duration)
|
|
|
|
txt.print(" milliseconds\n")
|
2024-07-21 23:05:58 +00:00
|
|
|
repeat {
|
|
|
|
}
|
2024-06-19 21:57:43 +00:00
|
|
|
}
|
|
|
|
}
|