fork(2) download
  1. N = 9
  2. tmp = [[0,0,0,2,0,0,0,6,3],
  3. [3,0,0,0,0,5,4,0,1],
  4. [0,0,1,0,0,3,9,8,0],
  5. [0,0,0,0,0,0,0,9,0],
  6. [0,0,0,5,3,8,0,0,0],
  7. [0,3,0,0,0,0,0,0,0],
  8. [0,2,6,3,0,0,5,0,0],
  9. [5,0,3,7,0,0,0,0,8],
  10. [4,7,0,0,0,1,0,0,0]]
  11. # solution: http://w...content-available-to-author-only...u.ws/hard-1-solution.htm
  12.  
  13. sud = [[int(x) for x in tmp[i]] for i in range(N)]
  14.  
  15. def check(x, y, val):
  16. if val in [i for i in sud[y]]: ### check row x for val
  17. return 1
  18. if val in [sud[i][x] for i in range(N)]: ### check column y for val
  19. return 1
  20. xbox = int(x/3) * 3
  21. ybox = int(y/3) * 3
  22. for i in range(xbox, xbox+3):
  23. for j in range(ybox, ybox+3):
  24. if val == sud[i][j]: ### check box for val
  25. return 1
  26. return 0
  27.  
  28. def solve(x = 0, y = 0):
  29. if x == N:
  30. y += 1
  31. x = 0
  32. if y == N:
  33. return 1
  34. if sud[y][x] > 0:
  35. return solve(x + 1, y)
  36. for i in range(1, N+1):
  37. if not check(x, y, i):
  38. sud[y][x] = i
  39. if solve(x + 1, y):
  40. print(sud)
  41. sud[y][x] = 0
  42. return 0
  43.  
  44. solve(0,0)
Success #stdin #stdout 0.04s 7848KB
stdin
Standard input is empty
stdout
Standard output is empty