Conversation
KubEF
reviewed
Oct 12, 2025
| 1) queen1(перебор) | ||
| - Изначально поставить ферзя n вариантов, далее (n-1) вариантов и т.д. O(n!) | ||
| - Для всех перестановок проверяется диагональ: два вложенных цикла это примерно O(n**2) | ||
| - Общая сложность O(n!*n**2) |
Collaborator
There was a problem hiding this comment.
Обычно в markdown для формул используют latex: $O(n! \cdot n^2)$
Comment on lines
+9
to
+10
| return 0 | ||
| return 1 |
Comment on lines
+1
to
+25
| """ | ||
| def queen(n): | ||
| cols = [None]*n | ||
| k = 0 | ||
|
|
||
| def diagonal(row, col): | ||
| for r in range(row): | ||
| if cols[r] == col or abs(cols[r] - col) == abs( | ||
| r - row | ||
| ): # вертикаль или диагональ совпадают | ||
| return 0 | ||
| return 1 | ||
|
|
||
| def solve(row=0): | ||
| nonlocal k | ||
| if row == n: | ||
| k += 1 | ||
| return | ||
| for col in range(n): | ||
| if diagonal(row, col): | ||
| cols[row] = col # ставим ферзя | ||
| solve(row + 1) # идём в следующую строку | ||
|
|
||
| solve() | ||
| return k |
Collaborator
There was a problem hiding this comment.
Предполагается, что вы придумаете сильно более быстрый алгоритм. Я дал подсказки некоторым из ваших одногруппников, проявите софт скиллы)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
add a task about queens