June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
31
Task/N-queens-problem/Sidef/n-queens-problem.sidef
Normal file
31
Task/N-queens-problem/Sidef/n-queens-problem.sidef
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
func N_queens_solution(N = 8) {
|
||||
|
||||
func collision(field, row) {
|
||||
for i in (^row) {
|
||||
var distance = (field[i] - field[row])
|
||||
distance ~~ [0, row-i, i-row] && return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func search(field, row) {
|
||||
row == N && return field
|
||||
for i in (^N) {
|
||||
field[row] = i
|
||||
if (!collision(field, row)) {
|
||||
return (__FUNC__(field, row+1) || next)
|
||||
}
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
for i in (0 .. N>>1) {
|
||||
if (var r = search([i], 1)) {
|
||||
return r
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for n in (1..15) {
|
||||
say "#{'%2d' % n}: #{N_queens_solution(n) || 'No solution'}"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue