Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions queens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# import itertools

# def is_valid(positions):
# for i in range(len(positions)):
# for j in range(i + 1, len(positions)):
# if abs(positions[i] - positions[j]) == j - i:
# return False
# return True

# def count_n_queens_bf(N):
# count = 0
# for perm in itertools.permutations(range(N)):
# if is_valid(perm):
# count += 1
# return count
#N = 4 # пример для N=8
#print(count_n_queens_bf(N))
Comment on lines +1 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этого быть не должно





def is_safe(queens, row, col):
for r, c in enumerate(queens):
if c == col or abs(r - row) == abs(c - col):
return False
return True

def backtrack(row, queens, N):
if row == N:
return 1
count = 0
for col in range(N):
if is_safe(queens, row, col):
count += backtrack(row + 1, queens + [col], N)
return count

def count_n_queens_bt(N):
return backtrack(0, [], N)

N = 4
print(count_n_queens_bt(N))


N = 4
print(count_n_queens_bit(N))