Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. sudokuTxt <- "
  2. 1 0 0 0 0 0 0 0 6
  3. 0 0 6 0 2 0 7 0 0
  4. 7 8 9 4 5 0 1 0 3
  5.  
  6. 0 0 0 8 0 7 0 0 4
  7. 0 0 0 0 3 0 0 0 0
  8. 0 9 0 0 0 4 2 0 1
  9.  
  10. 3 1 2 9 7 0 0 4 0
  11. 0 4 0 0 1 2 0 7 8
  12. 9 0 8 0 0 0 0 0 0"
  13.  
  14.  
  15. sudoku <- as.matrix(
  16. read.table(text = sudokuTxt,
  17. col.names = letters[1:9]))
  18.  
  19.  
  20. library(zeallot)
  21. library(magrittr)
  22.  
  23. solve <- function(partialSolution, choicesFUN) {
  24. # Eliminate impossible values, give some suggestions
  25. # and flag contradictions.
  26. c(partialSolution, suggestions, contradiction) %<-%
  27. eliminate(partialSolution, choicesFUN)
  28. # If dead end FALSE to trace back, if finished TRUE.
  29. if (contradiction) return(list(FALSE, NULL))
  30. if (all(partialSolution %in% 1:9))
  31. return(list(TRUE, partialSolution))
  32. # Branching, exit when the solution is found.
  33. for (suggestion in suggestions) {
  34. c(result, solution) %<-% solve(suggestion,
  35. choicesFUN)
  36. if (result) return(list(result, solution))
  37. }
  38. list(FALSE, NULL)
  39. }
  40.  
  41.  
  42. eliminate <- function(grid, choicesFUN) {
  43. suggestions <- 0:9
  44. for (i in 1:nrow(grid)) { for (j in 1:ncol(grid)) {
  45. if (grid[i, j] == 0L) {
  46. choices <- choicesFUN(grid, i, j)
  47. if (length(choices) == 0L) {
  48. return(list(NULL, NULL, TRUE))
  49. } else if (length(choices) == 1L) {
  50. grid[i, j] <- choices
  51. return(list(grid, list(grid), FALSE))
  52. } else
  53. suggestions <- updateSuggestions(
  54. choices, grid, i, j, suggestions)
  55. }
  56. }}
  57. list(grid, suggestions, FALSE)
  58. }
  59.  
  60.  
  61. # Find all the choices allowed by the rules.
  62. findChoices <- function(grid, i, j) {
  63. 1:9 %>% setdiff(grid[i, ]) %>%
  64. setdiff(grid[ , j]) %>%
  65. setdiff(grid[i - (i - 1) %% 3L + 0:2,
  66. j - (j - 1) %% 3L + 0:2])
  67. }
  68. # Create a list of grids with suggested next moves.
  69. updateSuggestions <- function(choices, grid, i, j,
  70. lastBest) {
  71. if (length(choices) < length(lastBest))
  72. lapply(choices, function(choice) {
  73. grid[i, j] <- choice; grid
  74. })
  75. else
  76. lastBest
  77. }
  78.  
  79.  
  80. solution <- solve(sudoku, findChoices)
  81. if (!solution[[1]]) { cat('Solution not found\n')
  82. } else { print(as.data.frame(solution[[2]])) }
  83.  
  84.  
  85. sudokuTxt <- "
  86. 8 0 0 0 0 0 0 0 0
  87. 0 0 3 6 0 0 0 0 0
  88. 0 7 0 0 9 0 2 0 0
  89. 0 5 0 0 0 7 0 0 0
  90. 0 0 0 0 4 5 7 0 0
  91. 0 0 0 1 0 0 0 3 0
  92. 0 0 1 0 0 0 0 6 8
  93. 0 0 8 5 0 0 0 1 0
  94. 0 9 0 0 0 0 4 0 0"
  95. sudoku <- as.matrix(
  96. read.table(text = sudokuTxt,
  97. col.names = letters[1:9]))
  98.  
  99.  
  100. solve2 <- function(partialSolution, choicesFUN) {
  101. # Eliminate impossible values, give some suggestions
  102. # and flag contradictions.
  103. elStep <- eliminate(partialSolution, choicesFUN)
  104. # If dead end FALSE to trace back, if finished TRUE.
  105. if (elStep[[3]]) return(list(res = FALSE,
  106. sol = NULL))
  107. if (all(elStep[[1]] %in% 1:9))
  108. return(list(res = TRUE, sol = elStep[[1]]))
  109. # Branching, exit when the solution is found.
  110. for (suggestion in elStep[[2]]) {
  111. ans <- solve2(suggestion, choicesFUN)
  112. if (ans$res) return(ans)
  113. }
  114. list(res = FALSE, sol = NULL)
  115. }
  116.  
  117.  
  118. findChoices2 <- function(grid, i, j) {
  119. setdiff(setdiff(setdiff(1:9,
  120. grid[i, ]),
  121. grid[ , j]),
  122. grid[i - (i - 1) %% 3L + 0:2,
  123. j - (j - 1) %% 3L + 0:2])
  124. }
  125.  
  126. print(microbenchmark::microbenchmark(
  127. solve2(sudoku, findChoices2),
  128. control = list(warmup = 20L)
  129. ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement