Created
February 8, 2024 22:26
-
-
Save Ranudar/864a7498fc8443a50697895ce09caf63 to your computer and use it in GitHub Desktop.
My solution to the 10 queens riddle, posed by Rodrigo from Mathspp
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
from collections import namedtuple | |
from copy import deepcopy | |
MIN_ROW, MAX_ROW = 0, 9 | |
MIN_COLUMN, MAX_COLUMN = 0, 9 | |
total_successful_combinations = 0 | |
total_failed_combinations = 0 | |
Field = namedtuple('Field', 'row, column') | |
board = [[" "] * 10 for column in range(10)] | |
def update_board(last_board, current_field=Field(0, 0)): | |
current_board = deepcopy(last_board) | |
# try placing a Queen onto the board | |
if current_board[current_field.row][current_field.column] == ' ': | |
current_board[current_field.row][current_field.column] = 'Q' | |
else: | |
# must be an 'X' if it's not an empty field | |
# print_board(current_board) | |
# print("No luck this time!\n") | |
global total_failed_combinations | |
total_failed_combinations += 1 | |
return | |
# check whether all Queens have been placed successfully | |
if current_field.row == 9: | |
print_board(current_board) | |
print("***Success!***\n") | |
global total_successful_combinations | |
total_successful_combinations += 1 | |
return | |
# get rid of options in same row | |
for col in range(MIN_COLUMN, MAX_COLUMN + 1): | |
if not Field(current_field.row, col) == current_field: | |
current_board[current_field.row][col] = 'X' | |
# get rid of options in same column | |
for row in range(MIN_ROW, MAX_ROW + 1): | |
if not Field(row, current_field.column) == current_field: | |
current_board[row][current_field.column] = 'X' | |
# get rid of options diagonally to the right | |
col = current_field.column | |
for row in range(current_field.row + 1, MAX_ROW + 1): | |
col += 1 | |
if col < MAX_COLUMN + 1 and not Field(row, col) == current_field: | |
current_board[row][col] = 'X' | |
# # get rid of options diagonally to the left | |
col = current_field.column | |
for row in range(current_field.row + 1, MAX_ROW + 1): | |
col -= 1 | |
if col > MIN_COLUMN - 1 and not Field(row, col) == current_field: | |
current_board[row][col] = 'X' | |
else: | |
break | |
# go to next line and call update_board recursively: | |
next_row = current_field.row + 1 | |
for col in range(MIN_COLUMN, MAX_COLUMN + 1): | |
next_field = Field(next_row, col) | |
next_board = current_board | |
update_board(next_board, next_field) | |
def print_board(board): | |
for col in board: | |
for char in col: | |
print(char, end=' ') | |
print() | |
def main(board): | |
for col in range(MIN_COLUMN, MAX_COLUMN + 1): | |
next_field = Field(0, col) | |
next_board = board | |
update_board(next_board, next_field) | |
print(f'{total_successful_combinations=} and {total_failed_combinations=}') | |
if __name__ == "__main__": | |
main(board) | |
# total_successful_combinations=724 and total_failed_combinations=312612 | |
# if the riddle is meant to also include all possible variations of different queens being placed in the same fields, then the answer must be multiplied by 10! = 3628800 (permutations without repetition) | |
# in this case the answer would be 2'627'251'200 possible combinations | |
# sample output: | |
""" | |
X X X X X X X X X Q | |
X X X X X X X Q X X | |
X X X X Q X X X X X | |
X X Q X X X X X X X | |
Q X X X X X X X X X | |
X X X X X Q X X X X | |
X Q X X X X X X X X | |
X X X X X X X X Q X | |
X X X X X X Q X X X | |
X X X Q X X X X X X | |
***Success!*** | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment