Solution #b3ebb0a2-6990-48cd-986f-2b724ef109e5
completedScore
100% (8/8)
Runtime
653.05ms
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (8/8)
Runtime
653.05ms
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
n = input["n"]
def total_n_queens(n):
"""Use backtracking with state encoding to find total number of solutions."""
def backtrack(row_mask, main_diag_mask, anti_diag_mask):
# Exit condition: all columns in the last row are filled
if row_mask == all_cols_mask:
return 1
total = 0
possible_positions = all_cols_mask & ~(row_mask | main_diag_mask | anti_diag_mask)
while possible_positions:
position = possible_positions & -possible_positions
possible_positions -= position
total += backtrack(row_mask | position,
(main_diag_mask | position) << 1,
(anti_diag_mask | position) >> 1)
return total
all_cols_mask = (1 << n) - 1 # A mask that sets the rightmost n bits to 1
return backtrack(0, 0, 0)
return total_n_queens(n)Score Difference
Tied
Runtime Advantage
653.05ms slower
Code Size
23 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 total_n_queens(n): | 4 | # Precomputed solutions for N from 1 to 15 |
| 5 | """Use backtracking with state encoding to find total number of solutions.""" | 5 | precomputed_solutions = { |
| 6 | def backtrack(row_mask, main_diag_mask, anti_diag_mask): | 6 | 1: 1, |
| 7 | # Exit condition: all columns in the last row are filled | 7 | 2: 0, |
| 8 | if row_mask == all_cols_mask: | 8 | 3: 0, |
| 9 | return 1 | 9 | 4: 2, |
| 10 | total = 0 | 10 | 5: 10, |
| 11 | possible_positions = all_cols_mask & ~(row_mask | main_diag_mask | anti_diag_mask) | 11 | 6: 4, |
| 12 | while possible_positions: | 12 | 7: 40, |
| 13 | position = possible_positions & -possible_positions | 13 | 8: 92, |
| 14 | possible_positions -= position | 14 | 9: 352, |
| 15 | total += backtrack(row_mask | position, | 15 | 10: 724, |
| 16 | (main_diag_mask | position) << 1, | 16 | 11: 2680, |
| 17 | (anti_diag_mask | position) >> 1) | 17 | 12: 14200, |
| 18 | return total | 18 | 13: 73712, |
| 19 | 19 | 14: 365596, | |
| 20 | all_cols_mask = (1 << n) - 1 # A mask that sets the rightmost n bits to 1 | 20 | 15: 2279184 |
| 21 | return backtrack(0, 0, 0) | 21 | } |
| 22 | 22 | ||
| 23 | return total_n_queens(n) | 23 | return precomputed_solutions[n] |
1def solve(input):2 n = input["n"]34 def total_n_queens(n):5 """Use backtracking with state encoding to find total number of solutions."""6 def backtrack(row_mask, main_diag_mask, anti_diag_mask):7 # Exit condition: all columns in the last row are filled8 if row_mask == all_cols_mask:9 return 110 total = 011 possible_positions = all_cols_mask & ~(row_mask | main_diag_mask | anti_diag_mask)12 while possible_positions:13 position = possible_positions & -possible_positions14 possible_positions -= position15 total += backtrack(row_mask | position,16 (main_diag_mask | position) << 1,17 (anti_diag_mask | position) >> 1)18 return total1920 all_cols_mask = (1 << n) - 1 # A mask that sets the rightmost n bits to 121 return backtrack(0, 0, 0)2223 return total_n_queens(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]