polygraph

Задача 8 ферзей (Las Vegas)

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