September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
56
Task/N-queens-problem/FreeBASIC/n-queens-problem.freebasic
Normal file
56
Task/N-queens-problem/FreeBASIC/n-queens-problem.freebasic
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
' version 13-04-2017
|
||||
' compile with: fbc -s console
|
||||
Dim Shared As ULong count, c()
|
||||
|
||||
Sub n_queens(row As ULong, n As ULong, show As ULong = 0)
|
||||
|
||||
Dim As ULong x, y
|
||||
|
||||
For x = 1 To n
|
||||
|
||||
For y = 1 To row -1
|
||||
If c(y) = x OrElse ((row - y) - Abs(x - c(y))) = 0 Then
|
||||
Continue For, For
|
||||
End If
|
||||
Next
|
||||
|
||||
c(row) = x
|
||||
If row < n Then
|
||||
n_queens(row +1 , n, show)
|
||||
Else
|
||||
count += 1
|
||||
|
||||
If show <> 0 Then
|
||||
For y = 1 To n
|
||||
Print Using "###"; c(y);
|
||||
Next
|
||||
Print
|
||||
End If
|
||||
|
||||
End If
|
||||
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As ULong n = 5
|
||||
ReDim c(n)
|
||||
' n_queens(1, n, show = 0 only show total | show <> 0 show every solution
|
||||
n_queens(1, n, 1)
|
||||
Print Using "## x ## board, ##### solutions"; n; n; count
|
||||
Print
|
||||
|
||||
For n = 1 To 14
|
||||
ReDim c(n)
|
||||
count = 0
|
||||
n_queens(1, n)
|
||||
Print Using "A ## x ## board has ######## solutions"; n; n; count
|
||||
Next
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
Loading…
Add table
Add a link
Reference in a new issue