Skip to content

Расстановка ферзей#4

Open
EStarikov wants to merge 2 commits intomainfrom
queens
Open

Расстановка ферзей#4
EStarikov wants to merge 2 commits intomainfrom
queens

Conversation

@EStarikov
Copy link
Owner

No description provided.

@EStarikov EStarikov requested a review from chernishev October 9, 2025 20:51
Copy link

@Godrik0 Godrik0 left a comment

Choose a reason for hiding this comment

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

Подумайте над решением за $O(n!)$, для этого стоит понять, как можно оптимизировать проверки в рекурсии. Поправьте стиль кода, надо бы вынести всё в отдельные функции.

Comment on lines +15 to +32
answer = 0

coordinates = []
for i in range(n):
for j in range(n):
coordinates.append([i, j]) #генерация всех возможных координат для ферзя
var = combinations(coordinates, n) #выбор n любых координат
for x in var:
flag = True
pool = [[0]*n for i in range(n)]
for i in range(n): #проверка что ферзи в данной выборке не бьют друг друга
row, column = x[i][0], x[i][1]
if not check(pool, row, column):
flag = False
break
pool[row][column] = 1
if flag == True:
answer += 1
Copy link

Choose a reason for hiding this comment

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

Такое стоит выносить в отдельную функцию

flag = False
break
pool[row][column] = 1
if flag == True:
Copy link

Choose a reason for hiding this comment

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

В Python булевые значения проверяют напрямую: if flag:

Comment on lines +18 to +21
for i in range(n):
for j in range(n):
coordinates.append([i, j]) #генерация всех возможных координат для ферзя
var = combinations(coordinates, n) #выбор n любых координат
Copy link

Choose a reason for hiding this comment

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

Это же очень неэффективно, для переборного стоит использовать permutations

return True

def next_row(r, current_board, n):
global k
Copy link

Choose a reason for hiding this comment

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

Использовать глобальные переменные -- плохая практика. В данном случае, следовало бы возвращать из функции количество найденных значений.

from itertools import permutations
def check(board, r, c):
for i in range(r):
if board[i] == c: # проверка столбца
Copy link

Choose a reason for hiding this comment

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

Мы же используем permutations, тут всегда будет False

Copy link

@Godrik0 Godrik0 left a comment

Choose a reason for hiding this comment

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

Линтер ругается.
Так же стоит его настроить.

Copy link

Choose a reason for hiding this comment

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

3: E302 expected 2 blank lines, found 1
14: E305 expected 2 blank lines after class or function definition, found 1
20: E262 inline comment should start with '# '
21: E262 inline comment should start with '# '
25: E262 inline comment should start with '# '
31: E712 comparison to True should be 'if cond is True
33: W292 no newline at end of file

Copy link

Choose a reason for hiding this comment

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

3: E262 inline comment should start with '# '
5: E262 inline comment should start with '# '
9: E302 expected 2 blank lines, found 1
18: E305 expected 2 blank lines after class or function definition, found 0
21: W292 no newline at end of file

Copy link

Choose a reason for hiding this comment

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

2: E302 expected 2 blank lines, found 0
23: E712 comparison to True should be 'if cond is True
25: W292 no newline at end of file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants