-
Notifications
You must be signed in to change notification settings - Fork 0
Queens: 3 solutions #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
60e8501
637f89b
f80bbed
5dbf9b4
9c02a7d
5fd0c58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <your_inline_code> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Что это? |
||
|
|
||
| Для первого, переборного решения: | ||
| 1. Цикл из функции is_valid_place "for first_place in range(n):" имеет сложность $O(N)$ | ||
| 2. В функции queen_placement "permutations" занимает O(N!) | ||
| 3. В добавок далее строится массив queens со сложностью O(N) | ||
|
|
||
| Итого: $O(N \cdot N! \cdot N)$ ~ $O(N^2 \cdot N!)$ | ||
|
|
||
| Для второго, рекурсивного решения: | ||
| 1. В функции trees_from_rows вызывется цикл перебора всех колонок N раз => $O(N)$ | ||
| 2. Рекурсивный вызов со сменой строки занимает $O(N - 1)$ (первую строку уже посчитали) | ||
|
|
||
| Итого: $O(N \cdot (N - 1))$ | ||
|
|
||
| Для третьего, чуть более оптимального решения со словарем: | ||
| I. Если N <= 15, то сложность программы - $O(c)$ | ||
| II. Если N >= 15, то сложность как и у второй программы - $O(N \cdot (N - 1))$ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| variations = [] | ||
|
|
||
|
|
||
| def queens_placement(side): | ||
| def trees_from_rows(row, columns, main_diagonal, collateral_diagonal): | ||
| if row == side: | ||
| return 1 | ||
|
|
||
| count = 0 | ||
|
|
||
| for column in range(side): | ||
| main = row - column | ||
| collateral = row + column | ||
|
|
||
| if ( | ||
| column not in columns | ||
| and (main) not in main_diagonal | ||
| and (collateral) not in collateral_diagonal | ||
| ): | ||
| columns.add(column) | ||
| main_diagonal.add(main) | ||
| collateral_diagonal.add(collateral) | ||
|
|
||
| count += trees_from_rows( | ||
| row + 1, columns, main_diagonal, collateral_diagonal | ||
| ) | ||
|
|
||
| columns.remove(column) | ||
| main_diagonal.remove(main) | ||
| collateral_diagonal.remove(collateral) | ||
|
|
||
| return count | ||
|
|
||
| return trees_from_rows(0, set(), set(), set()) | ||
|
|
||
|
|
||
| for i in range(15): | ||
| variations.append(queens_placement(i)) | ||
| print(variations) |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import itertools | ||
|
|
||
| user_side = int(input("Write the lenght of the side of the field: ")) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Под |
||
|
|
||
|
|
||
| def is_valid_place(positions): | ||
| for first_place in range(user_side): | ||
| for second_place in range(first_place + 1, user_side): | ||
| row1, column1 = positions[first_place] | ||
| row2, column2 = positions[second_place] | ||
| if abs(row1 - row2) == abs(column1 - column2): | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def queen_placement(side): | ||
| count = 0 | ||
| for rows in itertools.permutations(range(side)): | ||
| queens = [(row, column) for row, column in enumerate(rows)] | ||
| if is_valid_place(queens): | ||
| count += 1 | ||
| return count | ||
|
|
||
|
|
||
| print("Count of right placing: ", queen_placement(user_side)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| def queens_placement(side): | ||
| columns = set() | ||
| main_diagonal = set() | ||
| collateral_diagonal = set() | ||
|
|
||
| def trees_from_rows(row): | ||
| if row == side: | ||
| return 1 | ||
|
|
||
| count = 0 | ||
|
|
||
| for column in range(side): | ||
| main = row - column | ||
| collateral = row + column | ||
|
|
||
| if ( | ||
| column not in columns | ||
| and (main) not in main_diagonal | ||
| and (collateral) not in collateral_diagonal | ||
| ): | ||
| columns.add(column) | ||
| main_diagonal.add(main) | ||
| collateral_diagonal.add(collateral) | ||
|
|
||
| count += trees_from_rows(row + 1) | ||
|
|
||
| columns.remove(column) | ||
| main_diagonal.remove(main) | ||
| collateral_diagonal.remove(collateral) | ||
KubEF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return count | ||
|
|
||
| return trees_from_rows(0) | ||
|
|
||
|
|
||
| user_side = int(input("Write the value of the side of the field: ")) | ||
|
|
||
| print("Count of right placing: ", queens_placement(user_side)) | ||
|
Comment on lines
+36
to
+38
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Под |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| dictionary = [ | ||
| 0, | ||
| 1, | ||
| 0, | ||
| 0, | ||
| 2, | ||
| 10, | ||
| 4, | ||
| 40, | ||
| 92, | ||
| 352, | ||
| 724, | ||
| 2680, | ||
| 14200, | ||
| 73712, | ||
| 365596, | ||
| 2279184, | ||
| ] | ||
| from queens_on_the_table_2 import queens_placement | ||
|
|
||
|
|
||
| def queens_placement(side): | ||
| return trees_from_rows(0) | ||
|
|
||
|
|
||
| user_side = int(input("Write the value of the side of the field: ")) | ||
|
|
||
| if user_side <= len(dictionary): | ||
| print("Count of right placing: ", dictionary[user_side]) | ||
| else: | ||
| user_answer = input( | ||
| "Your number is large, and the calculation may take a VERY long time. Are you willing to wait? Y/N" | ||
| ) | ||
|
|
||
| if user_answer in "Yy": | ||
| queens_placement(user_side) | ||
| else: | ||
| print("Enter a smaller side value please") | ||
|
Comment on lines
+26
to
+38
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Под |
||
Uh oh!
There was an error while loading. Please reload this page.