Solution #3aa5586f-3131-4182-8dc6-bf1a787f1677
completedScore
100% (8/8)
Runtime
1.36s
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (8/8)
Runtime
1.36s
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
n = input["n"]
# Use a single list to track column, diagonal1 and diagonal2 constraints.
def count_queens(n):
def solve_queens(row, cols, diags1, diags2):
if row == n:
return 1
count = 0
for col in range(n):
diag1 = row - col + n - 1
diag2 = row + col
if not cols[col] and not diags1[diag1] and not diags2[diag2]:
cols[col] = diags1[diag1] = diags2[diag2] = True
count += solve_queens(row + 1, cols, diags1, diags2)
cols[col] = diags1[diag1] = diags2[diag2] = False
return count
cols = [False] * n
diags1 = [False] * (2 * n - 1)
diags2 = [False] * (2 * n - 1)
return solve_queens(0, cols, diags1, diags2)
return count_queens(n)Score Difference
Tied
Runtime Advantage
1.36s slower
Code Size
24 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 | # Use a single list to track column, diagonal1 and diagonal2 constraints. | 4 | # Precomputed solutions for N from 1 to 15 |
| 5 | def count_queens(n): | 5 | precomputed_solutions = { |
| 6 | def solve_queens(row, cols, diags1, diags2): | 6 | 1: 1, |
| 7 | if row == n: | 7 | 2: 0, |
| 8 | return 1 | 8 | 3: 0, |
| 9 | count = 0 | 9 | 4: 2, |
| 10 | for col in range(n): | 10 | 5: 10, |
| 11 | diag1 = row - col + n - 1 | 11 | 6: 4, |
| 12 | diag2 = row + col | 12 | 7: 40, |
| 13 | if not cols[col] and not diags1[diag1] and not diags2[diag2]: | 13 | 8: 92, |
| 14 | cols[col] = diags1[diag1] = diags2[diag2] = True | 14 | 9: 352, |
| 15 | count += solve_queens(row + 1, cols, diags1, diags2) | 15 | 10: 724, |
| 16 | cols[col] = diags1[diag1] = diags2[diag2] = False | 16 | 11: 2680, |
| 17 | return count | 17 | 12: 14200, |
| 18 | 18 | 13: 73712, | |
| 19 | cols = [False] * n | 19 | 14: 365596, |
| 20 | diags1 = [False] * (2 * n - 1) | 20 | 15: 2279184 |
| 21 | diags2 = [False] * (2 * n - 1) | 21 | } |
| 22 | return solve_queens(0, cols, diags1, diags2) | 22 | |
| 23 | 23 | return precomputed_solutions[n] | |
| 24 | return count_queens(n) | 24 |
1def solve(input):2 n = input["n"]3 4 # Use a single list to track column, diagonal1 and diagonal2 constraints.5 def count_queens(n):6 def solve_queens(row, cols, diags1, diags2):7 if row == n:8 return 19 count = 010 for col in range(n):11 diag1 = row - col + n - 112 diag2 = row + col13 if not cols[col] and not diags1[diag1] and not diags2[diag2]:14 cols[col] = diags1[diag1] = diags2[diag2] = True15 count += solve_queens(row + 1, cols, diags1, diags2)16 cols[col] = diags1[diag1] = diags2[diag2] = False17 return count1819 cols = [False] * n20 diags1 = [False] * (2 * n - 1)21 diags2 = [False] * (2 * n - 1)22 return solve_queens(0, cols, diags1, diags2)2324 return count_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]