diff --git a/src/complexity.md b/src/complexity.md new file mode 100644 index 0000000..5833ef2 --- /dev/null +++ b/src/complexity.md @@ -0,0 +1,18 @@ + + +Для первого, переборного решения: +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))$ \ No newline at end of file diff --git a/src/dictionary_queens.py b/src/dictionary_queens.py new file mode 100644 index 0000000..126d4a6 --- /dev/null +++ b/src/dictionary_queens.py @@ -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) diff --git a/src/euclid++.py b/src/euclid++.py deleted file mode 100644 index b3c6c5c..0000000 --- a/src/euclid++.py +++ /dev/null @@ -1,9 +0,0 @@ -def euclid(a, b): - if a == 0: - return (b, 0, 1) - gcd, x1, y1 = euclid(b % a, a) - koeff_x = y1 - (b//a) * x1 - koeff_y = x1 - return (gcd, koeff_x, koeff_y) - -print(euclid(240,46)) \ No newline at end of file diff --git a/src/hello.py b/src/hello.py deleted file mode 100644 index ed0f110..0000000 --- a/src/hello.py +++ /dev/null @@ -1 +0,0 @@ -print('hello') \ No newline at end of file diff --git a/src/queens_on_the_table_1.py b/src/queens_on_the_table_1.py new file mode 100644 index 0000000..bc0220c --- /dev/null +++ b/src/queens_on_the_table_1.py @@ -0,0 +1,25 @@ +import itertools + +user_side = int(input("Write the lenght of the side of the field: ")) + + +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)) diff --git a/src/queens_on_the_table_2.py b/src/queens_on_the_table_2.py new file mode 100644 index 0000000..ac9b8ef --- /dev/null +++ b/src/queens_on_the_table_2.py @@ -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)) diff --git a/src/queens_on_the_table_3.py b/src/queens_on_the_table_3.py new file mode 100644 index 0000000..1b8c056 --- /dev/null +++ b/src/queens_on_the_table_3.py @@ -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")