Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. '''
  2. #ez2
  3. board = [
  4. [9, 2, 6, 3, 4, 0, 7, 0, 1],
  5. [0, 5, 0, 0, 2, 6, 4, 0, 9],
  6. [0, 7, 0, 8, 0, 1, 0, 0, 0],
  7. [0, 0, 0, 9, 0, 0, 2, 0, 7],
  8. [3, 4, 2, 0, 0, 0, 0, 0, 5],
  9. [1, 0, 0, 0, 0, 0, 8, 0, 0],
  10. [6, 8, 5, 4, 0, 0, 0, 1, 2],
  11. [0, 0, 4, 0, 0, 2, 9, 0, 0],
  12. [0, 1, 0, 5, 3, 8, 0, 7, 0],
  13. ]
  14.  
  15. #ez
  16. board = [
  17. [7, 0, 0, 0, 0, 4, 2, 0, 1],
  18. [0, 3, 0, 0, 0, 9, 0, 0, 0],
  19. [0, 6, 0, 0, 7, 0, 0, 0, 5],
  20. [0, 1, 0, 0, 0, 0, 4, 0, 9],
  21. [8, 0, 0, 0, 6, 0, 0, 0, 2],
  22. [5, 0, 3, 0, 0, 0, 0, 6, 0],
  23. [1, 0, 0, 0, 8, 0, 0, 9, 0],
  24. [0, 0, 0, 7, 0, 0, 0, 7, 0],
  25. [9, 0, 7, 5, 0, 0, 0, 0, 6]
  26. ]
  27. '''
  28. #herd
  29. board = [
  30. [0, 0, 0, 6, 0, 0, 1, 0, 7],
  31. [6, 8, 0, 9, 5, 1, 3, 0, 0],
  32. [0, 0, 3, 0, 0, 2, 5, 0, 8],
  33. [0, 4, 0, 8, 1, 0, 0, 2, 0],
  34. [0, 0, 0, 0, 0, 0, 8, 5, 0],
  35. [0, 9, 0, 0, 6, 5, 0, 7, 3],
  36. [4, 0, 9, 0, 0, 3, 0, 8, 5],
  37. [1, 6, 2, 0, 0, 9, 0, 3, 0],
  38. [5, 0, 0, 7, 0, 6, 0, 0, 0]
  39. ]
  40.  
  41. def generate_board():
  42. base = 3
  43. side = base * base
  44. def pattern(r,c): return (base * (r % base) + r // base + c) % (side)
  45.  
  46. from random import sample
  47. def shuffle(s): return sample(s,len(s))
  48.  
  49. rBase = range(base)
  50. rows = [g * base + r for g in shuffle(rBase) for r in shuffle(rBase)]
  51. cols = [g * base + c for g in shuffle(rBase) for c in shuffle(rBase)]
  52. nums = shuffle(range(1,base * base + 1))
  53. #nums = range(1,base*base+1)
  54. board = [ [nums[pattern(r,c)] for c in cols] for r in rows ]
  55.  
  56. squares = side * side
  57. empties = squares * 3 // 4
  58. for p in sample(range(squares), empties):
  59. board[p // side][p % side] = 0
  60.  
  61. return board
  62.  
  63. def print_board(b):
  64. boardstr = ""
  65. for i in range(9):
  66. if i == 3 or i == 6:
  67. boardstr += "---------------------\n"
  68. for j in range(9):
  69. boardstr += str(b[i][j]) + " "
  70. if j == 8:
  71. boardstr += "\n"
  72. if j == 2 or j == 5:
  73. boardstr += "| "
  74. print(boardstr)
  75.  
  76. def is_valid_number(b, num, x, y):
  77. for i in range(9):
  78. if b[i][y] == num:
  79. return False
  80. for j in range(9):
  81. if b[x][j] == num:
  82. return False
  83. for i in range(3):
  84. for j in range(3):
  85. if b[(x // 3) * 3 + i][(y // 3) * 3 + j] == num:
  86. return False
  87. return True
  88.  
  89. def next_valid_position(b):
  90. for i in range(9):
  91. for j in range(9):
  92. if b[i][j] == 0:
  93. return (i, j)
  94. return True
  95.  
  96. def solve_board(b):
  97. if next_valid_position(b) != True:
  98. x, y = next_valid_position(b)
  99.  
  100. for i in range(1, 10):
  101. if is_valid_number(b, i, x, y):
  102. board[x][y] = i
  103. solve_board(board)
  104. board[x][y] = 0
  105. else:
  106. print("Board solved!\n")
  107. print_board(board)
  108. return
  109.  
  110. print("Generating Board...\n")
  111. board = generate_board()
  112. print_board(board)
  113. solve_board(board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement