Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions src/complexity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<your_inline_code>
Copy link
Collaborator

Choose a reason for hiding this comment

The 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))$
39 changes: 39 additions & 0 deletions src/dictionary_queens.py
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)
9 changes: 0 additions & 9 deletions src/euclid++.py

This file was deleted.

1 change: 0 additions & 1 deletion src/hello.py

This file was deleted.

25 changes: 25 additions & 0 deletions src/queens_on_the_table_1.py
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: "))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Под if __name__ == "__main__": это



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))
38 changes: 38 additions & 0 deletions src/queens_on_the_table_2.py
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)

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Под if __name__ == "__main__": это

38 changes: 38 additions & 0 deletions src/queens_on_the_table_3.py
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Под if __name__ == "__main__": это

Loading