September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
42
Task/N-queens-problem/F-Sharp/n-queens-problem-1.fs
Normal file
42
Task/N-queens-problem/F-Sharp/n-queens-problem-1.fs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
let rec iterate f value = seq {
|
||||
yield value
|
||||
yield! iterate f (f value) }
|
||||
|
||||
let up i = i + 1
|
||||
let right i = i
|
||||
let down i = i - 1
|
||||
|
||||
let noCollisionGivenDir solution number dir =
|
||||
Seq.forall2 (<>) solution (Seq.skip 1 (iterate dir number))
|
||||
|
||||
let goodAddition solution number =
|
||||
List.forall (noCollisionGivenDir solution number) [ up; right; down ]
|
||||
|
||||
let rec extendSolution n ps =
|
||||
[0..n - 1]
|
||||
|> List.filter (goodAddition ps)
|
||||
|> List.map (fun num -> num :: ps)
|
||||
|
||||
let allSolutions n =
|
||||
iterate (List.collect (extendSolution n)) [[]]
|
||||
|
||||
// Print one solution for the 8x8 case
|
||||
let printOneSolution () =
|
||||
allSolutions 8
|
||||
|> Seq.item 8
|
||||
|> Seq.head
|
||||
|> List.iter (fun rowIndex ->
|
||||
printf "|"
|
||||
[0..8] |> List.iter (fun i -> printf (if i = rowIndex then "X|" else " |"))
|
||||
printfn "")
|
||||
|
||||
// Print number of solution for the other cases
|
||||
let printNumberOfSolutions () =
|
||||
printfn "Size\tNr of solutions"
|
||||
[1..11]
|
||||
|> List.map ((fun i -> Seq.item i (allSolutions i)) >> List.length)
|
||||
|> List.iteri (fun i cnt -> printfn "%d\t%d" (i+1) cnt)
|
||||
|
||||
printOneSolution()
|
||||
|
||||
printNumberOfSolutions()
|
||||
21
Task/N-queens-problem/F-Sharp/n-queens-problem-2.fs
Normal file
21
Task/N-queens-problem/F-Sharp/n-queens-problem-2.fs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
| | | |X| | | | | |
|
||||
| |X| | | | | | | |
|
||||
| | | | | | |X| | |
|
||||
| | |X| | | | | | |
|
||||
| | | | | |X| | | |
|
||||
| | | | | | | |X| |
|
||||
| | | | |X| | | | |
|
||||
|X| | | | | | | | |
|
||||
|
||||
Size Nr of solutions
|
||||
1 1
|
||||
2 0
|
||||
3 0
|
||||
4 2
|
||||
5 10
|
||||
6 4
|
||||
7 40
|
||||
8 92
|
||||
9 352
|
||||
10 724
|
||||
11 2680
|
||||
Loading…
Add table
Add a link
Reference in a new issue