Skip to content

Commit b4ab798

Browse files
authored
Merge pull request #3 from fleron19/queens
Add queens hometask
2 parents 72b7452 + ec56716 commit b4ab798

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/queens/bruteforce.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import itertools
2+
3+
def is_valid(board):
4+
n = len(board)
5+
for i in range(n):
6+
for j in range(i + 1, n):
7+
if abs(board[i] - board[j]) == abs(i - j):
8+
return False
9+
return True
10+
11+
def n_queens_bruteforce(n):
12+
13+
solutions = 0
14+
for permutation in itertools.permutations(range(n)):
15+
if is_valid(permutation):
16+
solutions += 1
17+
return solutions
18+
19+
inp = int(input())
20+
print(n_queens_bruteforce(inp))
21+

src/queens/complexity.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Complexity analysis
2+
3+
### Bruteforce approach
4+
Itertools.permutations generates $n!$ permutations. Then, all of them is being checked using is_valid function
5+
this function checks every pair of given list, so its complexity is $O(n^2)$
6+
therefore, general complexity of brutforce method is $O(n! \cdot n^2)$
7+
8+
### Recursive approach
9+
This algorithm don't generate all possible cases and do not check each one. Instead, we generate only right variants using recusrion with backtraking, so this method do not check LOTS of variants that alredy considered wrong. But in worst case we still need to check all possible permutations, so compplexity of this approach is $O(n!)$
10+
11+
### Fastest approach
12+
This approach uses simple dictionary, where key of element is number of queens and value is solution for this number. So we need get 1 element from dict, and complexity of such action is $O(1)$

src/queens/fastest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
solutions = {0: 1, 1 : 1, 2 : 0, 3 : 0, 4 : 2, 5 : 10, 6 : 4, 7 : 40, 8 : 92, 9 : 352, 10 : 724,
2+
11 : 2680, 12 : 14200, 13 : 73712, 14 : 365596, 15 : 2279184, 16 : 14772512,
3+
17 : 95815104, 18 : 666090624, 19 : 4968057848, 20 : 39029188884,
4+
21 : 314666222712, 22 : 2691008701644, 23 : 24233937684440,
5+
24 : 227514171973736, 25 : 2207893435808352, 26 : 22317699616364044, 27 : 234907967154122528} # OEIS A000170
6+
inp = int(input())
7+
print(solutions[inp])

src/queens/recursion.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def n_queens_recursion(n):
2+
def backtrack(row, board, solutions):
3+
if row == n:
4+
solutions.append(board[:])
5+
return
6+
7+
for col in range(n):
8+
if is_safe(board, row, col):
9+
board[row] = col
10+
backtrack(row + 1, board, solutions)
11+
board[row] = -1
12+
13+
solutions = []
14+
board = [-1] * n
15+
backtrack(0, board, solutions)
16+
return solutions
17+
18+
def is_safe(board, row, col):
19+
for i in range(row):
20+
if board[i] == col or \
21+
abs(board[i] - col) == abs(i - row):
22+
return False
23+
return True
24+
inp = int(input())
25+
print(len(n_queens_recursion(inp)))

0 commit comments

Comments
 (0)