polygraph

Задача 8 ферзей (Поиск с возвратом)

Apr 21st, 2021 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. from random import randint
  2.  
  3. sols = 0
  4.  
  5. def f(k, e):
  6.     global sols
  7.     if k == 8:
  8.         print(e)
  9.         for x, _ in e:
  10.             print(('-' * x) + '*' + ('-' * (7 - x)))
  11.         sols += 1
  12.         return
  13.     for i in range(8):
  14.         for x, y in e:
  15.             if x == i or abs(y - k) == abs(x - i):
  16.                 break
  17.         else:
  18.             new_e = list(e)
  19.             new_e.append((i, k))
  20.             f(k + 1, new_e)
  21.  
  22. def main():
  23.     f(0, [])
  24.     print("solutions:", sols)
  25.  
  26. main()
  27.  
Add Comment
Please, Sign In to add comment