Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/N-queens-problem/R/n-queens-problem-1.r
Normal file
28
Task/N-queens-problem/R/n-queens-problem-1.r
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
queens <- function(n) {
|
||||
a <- seq(n)
|
||||
u <- rep(T, 2 * n - 1)
|
||||
v <- rep(T, 2 * n - 1)
|
||||
m <- NULL
|
||||
aux <- function(i) {
|
||||
if (i > n) {
|
||||
m <<- cbind(m, a)
|
||||
} else {
|
||||
for (j in seq(i, n)) {
|
||||
k <- a[[j]]
|
||||
p <- i - k + n
|
||||
q <- i + k - 1
|
||||
if (u[[p]] && v[[q]]) {
|
||||
u[[p]] <<- v[[q]] <<- F
|
||||
a[[j]] <<- a[[i]]
|
||||
a[[i]] <<- k
|
||||
aux(i + 1)
|
||||
u[[p]] <<- v[[q]] <<- T
|
||||
a[[i]] <<- a[[j]]
|
||||
a[[j]] <<- k
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
aux(1)
|
||||
m
|
||||
}
|
||||
3
Task/N-queens-problem/R/n-queens-problem-2.r
Normal file
3
Task/N-queens-problem/R/n-queens-problem-2.r
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
library(Matrix)
|
||||
a <- queens(8)
|
||||
as(a[, 1], "pMatrix")
|
||||
1
Task/N-queens-problem/R/n-queens-problem-3.r
Normal file
1
Task/N-queens-problem/R/n-queens-problem-3.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
sapply(4:12, function(n) ncol(queens(n)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue