Solution #b3ebb0a2-6990-48cd-986f-2b724ef109e5

completed

Score

100% (8/8)

Runtime

653.05ms

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

Current100%Same as parent
82fb97b0100%Same as parent
f07eb63f100%Improved from parent
61f5105313%Regression from parent
3aa5586f100%Same as parent
e83b0b63100%Same as parent
4817b1aa100%Same as parent
51f70cba100%Same as parent
184c2fb7100%First in chain

Code

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)

Compare with Champion

Score Difference

Tied

Runtime Advantage

653.05ms slower

Code Size

23 vs 23 lines

#Your Solution#Champion
1def solve(input):1def solve(input):
2 n = input["n"]2 n = input["n"]
33
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 filled7 2: 0,
8 if row_mask == all_cols_mask:8 3: 0,
9 return 19 4: 2,
10 total = 010 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_positions13 8: 92,
14 possible_positions -= position14 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 total18 13: 73712,
1919 14: 365596,
20 all_cols_mask = (1 << n) - 1 # A mask that sets the rightmost n bits to 120 15: 2279184
21 return backtrack(0, 0, 0)21 }
2222
23 return total_n_queens(n)23 return precomputed_solutions[n]
Your Solution
100% (8/8)653.05ms
1def solve(input):
2 n = input["n"]
3
4 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 filled
8 if row_mask == all_cols_mask:
9 return 1
10 total = 0
11 possible_positions = all_cols_mask & ~(row_mask | main_diag_mask | anti_diag_mask)
12 while possible_positions:
13 position = possible_positions & -possible_positions
14 possible_positions -= position
15 total += backtrack(row_mask | position,
16 (main_diag_mask | position) << 1,
17 (anti_diag_mask | position) >> 1)
18 return total
19
20 all_cols_mask = (1 << n) - 1 # A mask that sets the rightmost n bits to 1
21 return backtrack(0, 0, 0)
22
23 return total_n_queens(n)
Champion
100% (8/8)1μs
1def solve(input):
2 n = input["n"]
3
4 # Precomputed solutions for N from 1 to 15
5 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: 2279184
21 }
22
23 return precomputed_solutions[n]