Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/N-queens-problem/Groovy/n-queens-problem-1.groovy
Normal file
24
Task/N-queens-problem/Groovy/n-queens-problem-1.groovy
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
def listOrder = { a, b ->
|
||||
def k = [a.size(), b.size()].min()
|
||||
def i = (0..<k).find { a[it] != b[it] }
|
||||
(i != null) ? a[i] <=> b[i] : a.size() <=> b.size()
|
||||
}
|
||||
|
||||
def orderedPermutations = { list ->
|
||||
def n = list.size()
|
||||
(0..<n).permutations().sort(listOrder)
|
||||
}
|
||||
|
||||
def diagonalSafe = { list ->
|
||||
def n = list.size()
|
||||
n == 1 || (0..<(n-1)).every{ i ->
|
||||
((i+1)..<n).every{ j ->
|
||||
!([list[i]+j-i, list[i]+i-j].contains(list[j]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def queensDistinctSolutions = { n ->
|
||||
// each permutation is an N-Rooks solution
|
||||
orderedPermutations((0..<n)).findAll (diagonalSafe)
|
||||
}
|
||||
51
Task/N-queens-problem/Groovy/n-queens-problem-2.groovy
Normal file
51
Task/N-queens-problem/Groovy/n-queens-problem-2.groovy
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
class Reflect {
|
||||
public static final diag = { list ->
|
||||
final n = list.size()
|
||||
def tList = [0] * n
|
||||
(0..<n).each { tList[list[it]] = it }
|
||||
tList
|
||||
}
|
||||
|
||||
public static final vert = { list ->
|
||||
list.reverse()
|
||||
}
|
||||
|
||||
public static final horiz = { list ->
|
||||
final n = list.size()
|
||||
list.collect { n - it - 1 }
|
||||
}
|
||||
}
|
||||
|
||||
enum Rotations {
|
||||
r0([]),
|
||||
r90([Reflect.vert, Reflect.diag]),
|
||||
r180([Reflect.vert, Reflect.diag, Reflect.vert, Reflect.diag]),
|
||||
r270([Reflect.diag, Reflect.vert]);
|
||||
|
||||
private final List operations
|
||||
|
||||
private Rotations(List ops) {
|
||||
operations = ops ?: []
|
||||
}
|
||||
|
||||
public static void eliminateDups(primary, solutions) {
|
||||
(r0..r270).each { rot -> rot.eliminateDuplicates(primary, solutions) }
|
||||
}
|
||||
|
||||
private void eliminateDuplicates(primary, solutions) {
|
||||
def rotated = [] + primary
|
||||
operations.each { rotated = it(rotated) }
|
||||
solutions.removeAll([rotated, Reflect.vert(rotated)])
|
||||
}
|
||||
}
|
||||
|
||||
def queensUniqueSolutions = { start ->
|
||||
assert start instanceof Number || start instanceof List
|
||||
def qus = (start instanceof Number) \
|
||||
? queensDistinctSolutions(start) \
|
||||
: [] + start
|
||||
for (def i = 0; i < qus.size()-1; i++) {
|
||||
Rotations.eliminateDups(qus[i], qus[(i+1)..<(qus.size())])
|
||||
}
|
||||
qus
|
||||
}
|
||||
8
Task/N-queens-problem/Groovy/n-queens-problem-3.groovy
Normal file
8
Task/N-queens-problem/Groovy/n-queens-problem-3.groovy
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(1..9).each { n ->
|
||||
def qds = queensDistinctSolutions(n)
|
||||
def qus = queensUniqueSolutions(qds)
|
||||
println ([boardSize:n, "number of distinct solutions":qds.size(), "number of unique solutions":qus.size()])
|
||||
if(n < 9) { qus.each { println it } }
|
||||
else { println "first:${qus[0]}"; println "last:${qus[-1]}" }
|
||||
println()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue