tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
28
Task/N-queens-problem/R/n-queens-problem.r
Normal file
28
Task/N-queens-problem/R/n-queens-problem.r
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# Brute force, see the "Permutations" page for the next.perm function
|
||||
safe <- function(p) {
|
||||
n <- length(p)
|
||||
for(i in 1:(n-1)) {
|
||||
for(j in (i+1):n) {
|
||||
if(abs(p[j] - p[i]) == abs(j - i)) return(FALSE)
|
||||
}
|
||||
}
|
||||
return(TRUE)
|
||||
}
|
||||
|
||||
queens <- function(n) {
|
||||
p <- 1:n
|
||||
k <- 0
|
||||
while(!is.null(p)) {
|
||||
if(safe(p)) {
|
||||
cat(p,"\n")
|
||||
k <- k + 1
|
||||
}
|
||||
p <- next.perm(p)
|
||||
}
|
||||
return(k)
|
||||
}
|
||||
|
||||
queens(8)
|
||||
# 1 5 8 6 3 7 2 4
|
||||
# ...
|
||||
# 92
|
||||
Loading…
Add table
Add a link
Reference in a new issue