Paste #120367 |
pasted on 11.04.2019 12:03
- Edit to this paste
- Raw
- The following pastes replied to this paste: # 244098
- Show paste tree
-
Compare with paste
#
Text paste
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import numpy numQueens = 8 solutions = [] def board(placed): for c in placed: r = ['_ '] * 8 r[c] = '♕ ' print(''.join(r)) # строка из списка print('-' * 22) print(placed) def check(y1, x1, y2, x2): samev = x1 == x2 sameh = y1 == y2 maind = x1 + y1 == x2 + y2 secondd = x1 - y1 == x2 - y2 return samev or sameh or maind or secondd def invalid(placed, x2): for y, x in enumerate(placed): if check(y, x, len(placed), x2): return True def queens(valid, placed): if len(placed) == numQueens: solutions.append(placed[:]) return for y, x in enumerate(valid): if invalid(placed, x): continue placed.append(x) queens(list(valid[:y]) + list(valid[y+1:]), placed) placed.pop() print("a") a = int(input()) b = int(input()) print(solutions) queens(range(8), []) for solution in solutions: board(solution) print("Всего решений: ", len(solutions)) |