main.py 821 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import numpy as np
  2. def fill_and_mark(t, stud, pos):
  3. t[:, pos] = 'x'
  4. t[stud, :] = 'x'
  5. ys, xs = np.diag_indices_from(t)
  6. xs = (xs + pos) % len(t)
  7. ys = (ys + stud) % len(t)
  8. t[ys, xs] = 'x'
  9. t[stud, pos] = 'o'
  10. def main():
  11. n = 9
  12. t_init = np.full((n, n), ' ', dtype=str)
  13. fill_and_mark(t_init, 0, 0)
  14. sols = []
  15. stack = [(t_init, 1)]
  16. while stack:
  17. t, curr_stud = stack.pop()
  18. if curr_stud == n:
  19. sols.append(t)
  20. continue
  21. for pos in np.argwhere(t[curr_stud] == ' '):
  22. t_new = t.copy()
  23. fill_and_mark(t_new, curr_stud, pos)
  24. stack.append((t_new, curr_stud+1))
  25. print('number of solutions: ', len(sols))
  26. for solution in sols:
  27. print(solution)
  28. if __name__ == "__main__":
  29. main()