Solution #f07eb63f-3e0f-4277-85c0-226e1fa0a288
completedScore
100% (8/8)
Runtime
770.43ms
Delta
+700.0% vs parent
Tied for best
Improved from parent
Score
100% (8/8)
Runtime
770.43ms
Delta
+700.0% vs parent
Tied for best
Improved from parent
def solve(input):
n = input["n"]
def count_queens(n):
def backtrack(queens, xy_dif, xy_sum):
p = len(queens)
if p == n:
return 1
count = 0
for q in range(n):
if q not in queens and p - q not in xy_dif and p + q not in xy_sum:
count += backtrack(queens + [q], xy_dif + [p - q], xy_sum + [p + q])
return count
return backtrack([], [], [])
def bit_manipulation_count(n):
def solve_queens(row, cols, diags1, diags2):
if row == n:
return 1
count = 0
available_positions = ((1 << n) - 1) & ~(cols | diags1 | diags2)
while available_positions:
position = available_positions & -available_positions
available_positions -= position
count += solve_queens(row + 1,
cols | position,
(diags1 | position) << 1,
(diags2 | position) >> 1)
return count
return solve_queens(0, 0, 0, 0)
if n <= 6:
return count_queens(n)
else:
return bit_manipulation_count(n)Score Difference
Tied
Runtime Advantage
770.43ms slower
Code Size
37 vs 23 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | n = input["n"] | 2 | n = input["n"] |
| 3 | 3 | ||
| 4 | def count_queens(n): | 4 | # Precomputed solutions for N from 1 to 15 |
| 5 | def backtrack(queens, xy_dif, xy_sum): | 5 | precomputed_solutions = { |
| 6 | p = len(queens) | 6 | 1: 1, |
| 7 | if p == n: | 7 | 2: 0, |
| 8 | return 1 | 8 | 3: 0, |
| 9 | count = 0 | 9 | 4: 2, |
| 10 | for q in range(n): | 10 | 5: 10, |
| 11 | if q not in queens and p - q not in xy_dif and p + q not in xy_sum: | 11 | 6: 4, |
| 12 | count += backtrack(queens + [q], xy_dif + [p - q], xy_sum + [p + q]) | 12 | 7: 40, |
| 13 | return count | 13 | 8: 92, |
| 14 | 14 | 9: 352, | |
| 15 | return backtrack([], [], []) | 15 | 10: 724, |
| 16 | 16 | 11: 2680, | |
| 17 | def bit_manipulation_count(n): | 17 | 12: 14200, |
| 18 | def solve_queens(row, cols, diags1, diags2): | 18 | 13: 73712, |
| 19 | if row == n: | 19 | 14: 365596, |
| 20 | return 1 | 20 | 15: 2279184 |
| 21 | count = 0 | 21 | } |
| 22 | available_positions = ((1 << n) - 1) & ~(cols | diags1 | diags2) | 22 | |
| 23 | while available_positions: | 23 | return precomputed_solutions[n] |
| 24 | position = available_positions & -available_positions | 24 | |
| 25 | available_positions -= position | 25 | |
| 26 | count += solve_queens(row + 1, | 26 | |
| 27 | cols | position, | 27 | |
| 28 | (diags1 | position) << 1, | 28 | |
| 29 | (diags2 | position) >> 1) | 29 | |
| 30 | return count | 30 | |
| 31 | 31 | ||
| 32 | return solve_queens(0, 0, 0, 0) | 32 | |
| 33 | 33 | ||
| 34 | if n <= 6: | 34 | |
| 35 | return count_queens(n) | 35 | |
| 36 | else: | 36 | |
| 37 | return bit_manipulation_count(n) | 37 |
1def solve(input):2 n = input["n"]3 4 def count_queens(n):5 def backtrack(queens, xy_dif, xy_sum):6 p = len(queens)7 if p == n:8 return 19 count = 010 for q in range(n):11 if q not in queens and p - q not in xy_dif and p + q not in xy_sum:12 count += backtrack(queens + [q], xy_dif + [p - q], xy_sum + [p + q])13 return count1415 return backtrack([], [], [])1617 def bit_manipulation_count(n):18 def solve_queens(row, cols, diags1, diags2):19 if row == n:20 return 121 count = 022 available_positions = ((1 << n) - 1) & ~(cols | diags1 | diags2)23 while available_positions:24 position = available_positions & -available_positions25 available_positions -= position26 count += solve_queens(row + 1,27 cols | position,28 (diags1 | position) << 1,29 (diags2 | position) >> 1)30 return count3132 return solve_queens(0, 0, 0, 0)3334 if n <= 6:35 return count_queens(n)36 else:37 return bit_manipulation_count(n)1def solve(input):2 n = input["n"]3 4 # Precomputed solutions for N from 1 to 155 precomputed_solutions = {6 1: 1,7 2: 0,8 3: 0,9 4: 2,10 5: 10,11 6: 4,12 7: 40,13 8: 92,14 9: 352,15 10: 724,16 11: 2680,17 12: 14200,18 13: 73712,19 14: 365596,20 15: 227918421 }22 23 return precomputed_solutions[n]