-
Notifications
You must be signed in to change notification settings - Fork 0
Ферзи N на N #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
Open
stuffacc
wants to merge
3
commits into
main
Choose a base branch
from
t4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ферзи N на N #3
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import copy | ||
|
|
||
|
|
||
| # Здесь заполняем доску в зависимости от положения нового ферзя | ||
| # Доска, координаты ферзя | ||
| def fill_board(board, i_0, j_0): | ||
| # Здесь нужна копия, чтобы не менялся основной список | ||
| new_board = copy.deepcopy(board) | ||
|
|
||
| # Поставить ферзя | ||
| new_board[i_0][j_0] = 2 | ||
| count_empty = 0 | ||
|
|
||
| for i in range(len(new_board)): | ||
| for j in range(len(new_board[i])): | ||
| if new_board[i][j] == 2: | ||
| continue | ||
| # Ладья ходы | ||
| if i == i_0 or j == j_0: | ||
| new_board[i][j] = 1 | ||
| # Слон ходы | ||
| if abs(i - i_0) == abs(j - j_0): | ||
| new_board[i][j] = 1 | ||
| # Ладья + Слон = Ферзь | ||
|
|
||
|
|
||
| # Количество пустых клеток | ||
| if new_board[i][j] == 0: | ||
| count_empty += 1 | ||
|
|
||
| return new_board, count_empty | ||
|
|
||
| # Доска, количество ферзей на доске, координаты начала | ||
| def recursion(board, count, start_i=-1, start_j=-1): | ||
| # Если на доске N ферзей | ||
| if count == len(board): | ||
| # tuple, чтобы можно было добавить в set доску | ||
| tuple_board = tuple(tuple(row) for row in board) | ||
| a.add(tuple_board) | ||
| return None | ||
|
|
||
| # От каких координат начиннать | ||
| if start_i != -1 and start_j != -1: | ||
| new_board, count_empty = fill_board(board, start_i, start_j) | ||
|
|
||
| # Идти дальше, если свободных мест >= чем осталось поставить ферзей | ||
| if count_empty >= len(board) - (count + 1): | ||
| recursion(board=new_board, count=count + 1) | ||
|
|
||
| return None | ||
|
|
||
| # Если координаты не передали, то первый попавшийся | ||
| else: | ||
| for i in range(len(board)): | ||
| for j in range(len(board[i])): | ||
| # Если клетка пустая туда ферзя ставим | ||
| if board[i][j] == 0: | ||
| new_board, count_empty = fill_board(board, i, j) | ||
|
|
||
| # Идти дальше, если свободных мест >= чем осталось поставить ферзей | ||
| if count_empty >= len(board) - (count + 1): | ||
| recursion(board=new_board, count=count + 1) | ||
|
|
||
| return None | ||
|
|
||
| a = set() | ||
| n = int(input()) | ||
|
|
||
| # Перебрать все начальные клетки | ||
| for i in range(n): | ||
| for j in range(n): | ||
| board = [[0 for _ in range(n)] for _ in range(n)] | ||
| recursion(board=board, count=0, start_i=i, start_j=j) | ||
|
|
||
| print(len(a)) | ||
|
|
||
| # 7 за 26 секунд | ||
| # 8 за 14 минут | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| # решение 2 -------- O(n!) | ||
|
|
||
|
|
||
| def is_valid(board, row, col): | ||
| # Проверка наличия ферзя в данной строке | ||
| for i in range(col): | ||
| if board[row][i] == 1: | ||
| return False | ||
|
|
||
| # Проверка по главной диагонали | ||
| for i, j in zip(range(row, -1, -1), range(col, -1, -1)): | ||
| if board[i][j] == 1: | ||
| return False | ||
|
|
||
| # Проверка по побочной диагонали | ||
| for i, j in zip(range(row, len(board)), range(col, -1, -1)): | ||
| if board[i][j] == 1: | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| def solve_n_queens_util(board, col): | ||
| # Если все ферзи расставлены | ||
| if col >= len(board): | ||
| return 1 | ||
|
|
||
| count = 0 | ||
| for row in range(len(board)): | ||
| if is_valid(board, row, col): | ||
| # Расставление ферзя | ||
| board[row][col] = 1 | ||
|
|
||
| # В следующий столбец | ||
| count += solve_n_queens_util(board, col + 1) | ||
| # Удаление ферзя | ||
| board[row][col] = 0 | ||
|
|
||
| return count | ||
|
|
||
| def solve_n_queens(n): | ||
| board = [[0 for _ in range(n)] for _ in range(n)] | ||
| return solve_n_queens_util(board, 0) | ||
|
|
||
| print(solve_n_queens(n)) | ||
|
|
||
| # Решение 3 ---------- O(1) | ||
|
|
||
| def solve_n_queens_constant(n): | ||
| solutions = { | ||
| 1: 1, | ||
| 2: 0, | ||
| 3: 0, | ||
| 4: 2, | ||
| 5: 10, | ||
| 6: 4, | ||
| 7: 40, | ||
| 8: 92, | ||
| 9: 352, | ||
| 10: 724, | ||
| 11: 2680, | ||
| 12: 14200, | ||
| 13: 73712, | ||
| 14: 365596, | ||
| 15: 2279184, | ||
| 16: 14772512, | ||
| 17: 95815104, | ||
| 18: 666090624, | ||
| 19: 4968057848, | ||
| 20: 39029188884, | ||
| 21: 314666222712, | ||
| 22: 2691008701644, | ||
| 23: 24233937684440, | ||
| 24: 227514171973736, | ||
| 25: 2207893435808352, | ||
| 26: 22317699616364044, | ||
| 27: 234907967154122528 | ||
| } | ||
| return solutions.get(n, 0) | ||
|
|
||
| print(solve_n_queens_constant(n)) | ||
|
|
||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| 1. Рекурсивное решение: O(n^(n+4)) | ||
|
|
||
| 2. Переборное решение: O(n!) | ||
|
|
||
| 3. самое быстрое решение, которое получилось - O(1) | ||
|
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Но ведь это тоже рекурсивное решение, а в
complexity.mdназвано переборным