-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueens.py
38 lines (31 loc) · 1.02 KB
/
queens.py
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
# assignment: n-Queens
# author: Will Sobolewski
# date: Aug 2nd, 2023
# file: queens.py is a program that solves the n-queens puzzle
# input: number of queens
# output: number of solutions and all solutions
def all_perms(elements):
if len(elements) <= 1:
yield elements
else:
for perm in all_perms(elements[1:]):
for i in range(len(elements)):
yield perm[:i] + elements[0:1] + perm[i:]
def is_solution(perm):
for i in range(len(perm)):
for j in range(i + 1, len(perm)):
if abs(i - j) == abs(perm[i] - perm[j]):
return False
return True
def solve_queens(n):
solutions = []
for perm in all_perms(list(range(1, n + 1))):
if is_solution(perm):
solutions.append(perm)
return solutions
if __name__ == '__main__':
n = int(input('Enter a number of queens: \n'))
solutions = solve_queens(n)
print(f'The {n}-queens puzzle has {len(solutions)} solutions:')
for solution in solutions:
print(solution)