Solution #51f70cba-2ee0-4385-836a-fa92b7f91b95

completed

Score

100% (8/8)

Runtime

1.49s

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

Current100%Same as parent
184c2fb7100%First in chain

Code

def solve(input):
    n = input["n"]
    
    class NQueensCounter:
        __slots__ = 'n', 'count', 'cols', 'diags1', 'diags2'

        def __init__(self, n):
            self.n = n
            self.count = 0
            self.cols = [False] * n
            self.diags1 = [False] * (2 * n - 1)
            self.diags2 = [False] * (2 * n - 1)

        def solve(self, row=0):
            if row == self.n:
                self.count += 1
                return
            for col in range(self.n):
                diag1 = row - col + self.n - 1
                diag2 = row + col
                if not self.cols[col] and not self.diags1[diag1] and not self.diags2[diag2]:
                    self.cols[col] = self.diags1[diag1] = self.diags2[diag2] = True
                    self.solve(row + 1)
                    self.cols[col] = self.diags1[diag1] = self.diags2[diag2] = False

    counter = NQueensCounter(n)
    counter.solve()
    return counter.count

Compare with Champion

Score Difference

Tied

Runtime Advantage

1.49s slower

Code Size

28 vs 23 lines

#Your Solution#Champion
1def solve(input):1def solve(input):
2 n = input["n"]2 n = input["n"]
3 3
4 class NQueensCounter:4 # Precomputed solutions for N from 1 to 15
5 __slots__ = 'n', 'count', 'cols', 'diags1', 'diags2'5 precomputed_solutions = {
66 1: 1,
7 def __init__(self, n):7 2: 0,
8 self.n = n8 3: 0,
9 self.count = 09 4: 2,
10 self.cols = [False] * n10 5: 10,
11 self.diags1 = [False] * (2 * n - 1)11 6: 4,
12 self.diags2 = [False] * (2 * n - 1)12 7: 40,
1313 8: 92,
14 def solve(self, row=0):14 9: 352,
15 if row == self.n:15 10: 724,
16 self.count += 116 11: 2680,
17 return17 12: 14200,
18 for col in range(self.n):18 13: 73712,
19 diag1 = row - col + self.n - 119 14: 365596,
20 diag2 = row + col20 15: 2279184
21 if not self.cols[col] and not self.diags1[diag1] and not self.diags2[diag2]:21 }
22 self.cols[col] = self.diags1[diag1] = self.diags2[diag2] = True22
23 self.solve(row + 1)23 return precomputed_solutions[n]
24 self.cols[col] = self.diags1[diag1] = self.diags2[diag2] = False24
2525
26 counter = NQueensCounter(n)26
27 counter.solve()27
28 return counter.count28
Your Solution
100% (8/8)1.49s
1def solve(input):
2 n = input["n"]
3
4 class NQueensCounter:
5 __slots__ = 'n', 'count', 'cols', 'diags1', 'diags2'
6
7 def __init__(self, n):
8 self.n = n
9 self.count = 0
10 self.cols = [False] * n
11 self.diags1 = [False] * (2 * n - 1)
12 self.diags2 = [False] * (2 * n - 1)
13
14 def solve(self, row=0):
15 if row == self.n:
16 self.count += 1
17 return
18 for col in range(self.n):
19 diag1 = row - col + self.n - 1
20 diag2 = row + col
21 if not self.cols[col] and not self.diags1[diag1] and not self.diags2[diag2]:
22 self.cols[col] = self.diags1[diag1] = self.diags2[diag2] = True
23 self.solve(row + 1)
24 self.cols[col] = self.diags1[diag1] = self.diags2[diag2] = False
25
26 counter = NQueensCounter(n)
27 counter.solve()
28 return counter.count
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]