Solution #51f70cba-2ee0-4385-836a-fa92b7f91b95
completedScore
100% (8/8)
Runtime
1.49s
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (8/8)
Runtime
1.49s
Delta
No change vs parent
Tied for best
Same as parent
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.countScore Difference
Tied
Runtime Advantage
1.49s slower
Code Size
28 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 | class NQueensCounter: | 4 | # Precomputed solutions for N from 1 to 15 |
| 5 | __slots__ = 'n', 'count', 'cols', 'diags1', 'diags2' | 5 | precomputed_solutions = { |
| 6 | 6 | 1: 1, | |
| 7 | def __init__(self, n): | 7 | 2: 0, |
| 8 | self.n = n | 8 | 3: 0, |
| 9 | self.count = 0 | 9 | 4: 2, |
| 10 | self.cols = [False] * n | 10 | 5: 10, |
| 11 | self.diags1 = [False] * (2 * n - 1) | 11 | 6: 4, |
| 12 | self.diags2 = [False] * (2 * n - 1) | 12 | 7: 40, |
| 13 | 13 | 8: 92, | |
| 14 | def solve(self, row=0): | 14 | 9: 352, |
| 15 | if row == self.n: | 15 | 10: 724, |
| 16 | self.count += 1 | 16 | 11: 2680, |
| 17 | return | 17 | 12: 14200, |
| 18 | for col in range(self.n): | 18 | 13: 73712, |
| 19 | diag1 = row - col + self.n - 1 | 19 | 14: 365596, |
| 20 | diag2 = row + col | 20 | 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] = True | 22 | |
| 23 | self.solve(row + 1) | 23 | return precomputed_solutions[n] |
| 24 | self.cols[col] = self.diags1[diag1] = self.diags2[diag2] = False | 24 | |
| 25 | 25 | ||
| 26 | counter = NQueensCounter(n) | 26 | |
| 27 | counter.solve() | 27 | |
| 28 | return counter.count | 28 |
1def solve(input):2 n = input["n"]3 4 class NQueensCounter:5 __slots__ = 'n', 'count', 'cols', 'diags1', 'diags2'67 def __init__(self, n):8 self.n = n9 self.count = 010 self.cols = [False] * n11 self.diags1 = [False] * (2 * n - 1)12 self.diags2 = [False] * (2 * n - 1)1314 def solve(self, row=0):15 if row == self.n:16 self.count += 117 return18 for col in range(self.n):19 diag1 = row - col + self.n - 120 diag2 = row + col21 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] = True23 self.solve(row + 1)24 self.cols[col] = self.diags1[diag1] = self.diags2[diag2] = False2526 counter = NQueensCounter(n)27 counter.solve()28 return counter.count1def 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]