-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-Queens.py
77 lines (59 loc) · 2.32 KB
/
N-Queens.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def check_threats(chess_board, square):
""" board is a matrix containing the entire chessboard at this moment, every square with a 0 value is empty and
every square with a 1 value has a queen on it.
square is the coordinates of a target square on the board that we are trying to check the safety of, it's a list
containing two integers as the row and column
This function returns False if there are no queens threatening the one in square
:param chess_board:
:param square:
:return bool:
"""
row = square[0]
column = square[1]
for i in range(0, len(chess_board)):
for j in range(0, len(chess_board)):
if chess_board[i][j] == 1:
if i == row or j == column or abs(i - row) == abs(j - column):
return True
return False
def initialize_board(size):
# Initializing the chessboard
chess_board = []
for i in range(size):
tmp_board = [0] * size
chess_board.append(tmp_board)
# Chessboard initialized, all the squares are empty therefore equal to 0, once there's a queen inside they will
# be shifted to 1
return chess_board
result = []
board = initialize_board(4)
def n_queens(row=0, column=0):
"""
This function uses a backtracking algorithm to solve the n-queens problem where we are given n queens and an n*n
chessboard and are asked to place the queens in a way that none of them threaten each other.
The function prints all possible answers in a list named result meaning every result[i]=j means that there's a queen
in the i row and the j column.
:param row:
:param column:
:return:
"""
if row >= len(board) or column >= len(board):
raise IndexError(board, row, column)
if row == len(board) - 1 and not check_threats(board, [row, column]):
result.append(column)
result2 = []
for i in result:
result2.append(i+1)
print(result2)
result.pop()
return
if not check_threats(board, [row, column]):
result.append(column)
board[row][column] = 1
n_queens(row + 1, 0)
result.pop()
board[row][column] = 0
if column == len(board) - 1:
return
n_queens(row, column + 1)
n_queens()