sudoku_solver_backtracking.jl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/julia
  2. # Author: Trizen
  3. # Date: 12 February 2024
  4. # https://github.com/trizen
  5. # Solve Sudoku puzzle (recursive/backtracking solution).
  6. function is_valid(board, row, col, num)
  7. # Check if the number is not present in the current row and column
  8. for i in 1:9
  9. if (board[row][i] == num) || (board[i][col] == num)
  10. return false
  11. end
  12. end
  13. # Check if the number is not present in the current 3x3 subgrid
  14. start_row, start_col = 3*div(row-1, 3), 3*div(col-1, 3)
  15. for i in 1:3, j in 1:3
  16. if board[start_row + i][start_col + j] == num
  17. return false
  18. end
  19. end
  20. return true
  21. end
  22. function find_empty_location(board)
  23. # Find an empty positions (cell with 0)
  24. for i in 1:9, j in 1:9
  25. if board[i][j] == 0
  26. return [i,j]
  27. end
  28. end
  29. return (nothing, nothing)
  30. end
  31. function solve_sudoku(board)
  32. row, col = find_empty_location(board)
  33. if (row == nothing && col == nothing)
  34. return true # Puzzle is solved
  35. end
  36. for num in 1:9
  37. if is_valid(board, row, col, num)
  38. # Try placing the number
  39. board[row][col] = num
  40. # Recursively try to solve the rest of the puzzle
  41. if solve_sudoku(board)
  42. return true
  43. end
  44. # If placing the current number doesn't lead to a solution, backtrack
  45. board[row][col] = 0
  46. end
  47. end
  48. return false # No solution found
  49. end
  50. # Example usage:
  51. # Define the Sudoku puzzle as a 9x9 list with 0 representing empty cells
  52. sudoku_board = [
  53. [2, 0, 0, 0, 7, 0, 0, 0, 3],
  54. [1, 0, 0, 0, 0, 0, 0, 8, 0],
  55. [0, 0, 4, 2, 0, 9, 0, 0, 5],
  56. [9, 4, 0, 0, 0, 0, 6, 0, 8],
  57. [0, 0, 0, 8, 0, 0, 0, 9, 0],
  58. [0, 0, 0, 0, 0, 0, 0, 7, 0],
  59. [7, 2, 1, 9, 0, 8, 0, 6, 0],
  60. [0, 3, 0, 0, 2, 7, 1, 0, 0],
  61. [4, 0, 0, 0, 0, 3, 0, 0, 0]
  62. ]
  63. if false
  64. sudoku_board = [
  65. [0, 0, 0, 8, 0, 1, 0, 0, 0],
  66. [0, 0, 0, 0, 0, 0, 0, 4, 3],
  67. [5, 0, 0, 0, 0, 0, 0, 0, 0],
  68. [0, 0, 0, 0, 7, 0, 8, 0, 0],
  69. [0, 0, 0, 0, 0, 0, 1, 0, 0],
  70. [0, 2, 0, 0, 3, 0, 0, 0, 0],
  71. [6, 0, 0, 0, 0, 0, 0, 7, 5],
  72. [0, 0, 3, 4, 0, 0, 0, 0, 0],
  73. [0, 0, 0, 2, 0, 0, 6, 0, 0]
  74. ]
  75. end
  76. if false
  77. sudoku_board = [
  78. [8, 0, 0, 0, 0, 0, 0, 0, 0],
  79. [0, 0, 3, 6, 0, 0, 0, 0, 0],
  80. [0, 7, 0, 0, 9, 0, 2, 0, 0],
  81. [0, 5, 0, 0, 0, 7, 0, 0, 0],
  82. [0, 0, 0, 0, 4, 5, 7, 0, 0],
  83. [0, 0, 0, 1, 0, 0, 0, 3, 0],
  84. [0, 0, 1, 0, 0, 0, 0, 6, 8],
  85. [0, 0, 8, 5, 0, 0, 0, 1, 0],
  86. [0, 9, 0, 0, 0, 0, 4, 0, 0]
  87. ]
  88. end
  89. solved = solve_sudoku(sudoku_board)
  90. if (solved)
  91. for row in sudoku_board
  92. println(row)
  93. end
  94. else
  95. println("No solution exists.")
  96. end