Algorithms: Backtracking

Algorithms: Backtracking

Explore algorithms: backtracking code snippets and tutorials

Algorithms: Backtracking

N Queens Backtracking in C

N-queens places one queen in each row with no shared columns or diagonals..

c
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <errno.h>
// N Queens Backtracking in C
//
// PURPOSE
// N-queens places one queen in each row with no shared columns or diagonals.
//
// METHOD
// Try columns row by row. Keep occupied-column and diagonal marker arrays for constant-time
// safety checks. Backtrack when no column remains and count complete placements.
//
// COMPLEXITY
// Exponential search, bounded by O(n!) candidate placements with additional column-scan
// overhead; O(n) auxiliary space.
//
// CONTRACT
// 0<=n<=12. Returns solution count. The empty board has one placement. This implementation
// avoids recursion.
// Public APIs infer array lengths except in C, which accepts pointer/length pairs. Vertex
// counts and matrix dimensions remain explicit. Arrays must contain signed 32-bit integers.
// Sorted order, correct tree topology, and caller-owned data-structure invariants are
// preconditions; checking them on every query would change query complexity.
// Scalar and shape checks are included. Value-range checks are included where needed to
// prevent invalid indexing or nontermination. Allocation sizes are capped at 4,000,000
// workspace entries. Arithmetic outside Python raises an error on 32-bit overflow; Python
// uses arbitrary precision. Native allocation failures propagate. In-place outputs may be
// partly updated after arithmetic failure. These are single-threaded, in-memory
// implementations.
//
// LANGUAGE DETAILS
// C99: checked calloc/free workspace, const input pointers, explicit size_t lengths. Errors
// return INT_MIN with errno=EINVAL, ERANGE, or ENOMEM. Check errno before using a result.
// Input/output arrays must not alias. No variable-length stack arrays.
//
// EXAMPLE
// Expected return: 1.
// Run: gcc -std=c99 -O2 example.c -o example && ./example
// Tests for this exact source artifact run before publication. The accompanying source
// archive includes unit cases, runtime results, and source hashes.

static int alg_min(int a, int b) { return a < b ? a : b; }
static int alg_max(int a, int b) { return a > b ? a : b; }
static int alg_checked(int64_t v, int *error) {
    if (v < INT_MIN || v > INT_MAX) { *error = 1; return 0; }
    return (int)v;
}
static int alg_add(int a, int b, int *e) { return alg_checked((int64_t)a+b,e); }
static int alg_sub(int a, int b, int *e) { return alg_checked((int64_t)a-b,e); }
static int alg_mul(int a, int b, int *e) { return alg_checked((int64_t)a*b,e); }

int n_queens(int n) {
    int alg_error = 0;
    int alg_result = INT_MIN;
    errno = 0;
    int *columns = NULL;
    int *rising = NULL;
    int *falling = NULL;
    int *placement = NULL;
    if (!(((n >= 0) && (n <= 1000000)))) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    if (!(((n >= 0) && (n <= 12)))) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    {
        int alg_length = alg_add(n, 1, &alg_error);
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        if (alg_length < 0 || alg_length > 4000000) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
        columns = calloc((size_t)(alg_length ? alg_length : 1), sizeof(*columns));
        if (!columns) { errno = ENOMEM; alg_result = INT_MIN; goto alg_cleanup; }
    }
    {
        int alg_length = alg_add(alg_mul(2, n, &alg_error), 1, &alg_error);
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        if (alg_length < 0 || alg_length > 4000000) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
        rising = calloc((size_t)(alg_length ? alg_length : 1), sizeof(*rising));
        if (!rising) { errno = ENOMEM; alg_result = INT_MIN; goto alg_cleanup; }
    }
    {
        int alg_length = alg_add(alg_mul(2, n, &alg_error), 1, &alg_error);
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        if (alg_length < 0 || alg_length > 4000000) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
        falling = calloc((size_t)(alg_length ? alg_length : 1), sizeof(*falling));
        if (!falling) { errno = ENOMEM; alg_result = INT_MIN; goto alg_cleanup; }
    }
    {
        int alg_length = alg_add(n, 1, &alg_error);
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        if (alg_length < 0 || alg_length > 4000000) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
        placement = calloc((size_t)(alg_length ? alg_length : 1), sizeof(*placement));
        if (!placement) { errno = ENOMEM; alg_result = INT_MIN; goto alg_cleanup; }
    }
    int row = 0;
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    int col = 0;
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    int count = 0;
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    int alg_condition_1 = (n == 0);
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    if (alg_condition_1) {
        alg_result = 1;
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        goto alg_cleanup;
    }
    while (1) {
        int alg_condition = (row >= 0);
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        if (!alg_condition) break;
        col = placement[row];
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        int alg_condition_2 = (col > 0);
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        if (alg_condition_2) {
            columns[alg_sub(col, 1, &alg_error)] = 0;
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            rising[alg_sub(alg_add(row, col, &alg_error), 1, &alg_error)] = 0;
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            falling[alg_add(alg_sub(row, col, &alg_error), n, &alg_error)] = 0;
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        }
        while (1) {
            int alg_condition = ((col < n) && ((columns[col] != 0) || (rising[alg_add(row, col, &alg_error)] != 0) || (falling[alg_sub(alg_add(alg_sub(row, col, &alg_error), n, &alg_error), 1, &alg_error)] != 0)));
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            if (!alg_condition) break;
            col = alg_add(col, 1, &alg_error);
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        }
        int alg_condition_3 = (col == n);
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        if (alg_condition_3) {
            placement[row] = 0;
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            row = alg_sub(row, 1, &alg_error);
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        } else {
            placement[row] = alg_add(col, 1, &alg_error);
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            columns[col] = 1;
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            rising[alg_add(row, col, &alg_error)] = 1;
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            falling[alg_sub(alg_add(alg_sub(row, col, &alg_error), n, &alg_error), 1, &alg_error)] = 1;
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            int alg_condition_4 = (row == alg_sub(n, 1, &alg_error));
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            if (alg_condition_4) {
                count = alg_add(count, 1, &alg_error);
                if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            } else {
                row = alg_add(row, 1, &alg_error);
                if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
                placement[row] = 0;
                if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            }
        }
    }
    alg_result = count;
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    goto alg_cleanup;
alg_cleanup:
    free(columns);
    free(rising);
    free(falling);
    free(placement);
    return alg_result;
}

int main(void) {
    int n = 0;
    int result = n_queens(n);
    printf("%d\n", result);
    if (errno) { perror("algorithm"); return 1; }
    return 0;
}
Algorithms: Backtracking

N Queens Backtracking in C++

N-queens places one queen in each row with no shared columns or diagonals..

cpp
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <cstdint>
#include <limits>
// N Queens Backtracking in C++
//
// PURPOSE
// N-queens places one queen in each row with no shared columns or diagonals.
//
// METHOD
// Try columns row by row. Keep occupied-column and diagonal marker arrays for constant-time
// safety checks. Backtrack when no column remains and count complete placements.
//
// COMPLEXITY
// Exponential search, bounded by O(n!) candidate placements with additional column-scan
// overhead; O(n) auxiliary space.
//
// CONTRACT
// 0<=n<=12. Returns solution count. The empty board has one placement. This implementation
// avoids recursion.
// Public APIs infer array lengths except in C, which accepts pointer/length pairs. Vertex
// counts and matrix dimensions remain explicit. Arrays must contain signed 32-bit integers.
// Sorted order, correct tree topology, and caller-owned data-structure invariants are
// preconditions; checking them on every query would change query complexity.
// Scalar and shape checks are included. Value-range checks are included where needed to
// prevent invalid indexing or nontermination. Allocation sizes are capped at 4,000,000
// workspace entries. Arithmetic outside Python raises an error on 32-bit overflow; Python
// uses arbitrary precision. Native allocation failures propagate. In-place outputs may be
// partly updated after arithmetic failure. These are single-threaded, in-memory
// implementations.
//
// LANGUAGE DETAILS
// C++17: const vector references for inputs, mutable references for outputs, RAII workspace,
// standard lower_bound for LIS. Invalid inputs throw invalid_argument; overflow throws
// overflow_error; allocation failure propagates bad_alloc.
//
// EXAMPLE
// Expected return: 1.
// Run: g++ -std=c++17 -O2 example.cpp -o example && ./example
// Tests for this exact source artifact run before publication. The accompanying source
// archive includes unit cases, runtime results, and source hashes.

static int alg_min(int a, int b) { return a < b ? a : b; }
static int alg_max(int a, int b) { return a > b ? a : b; }
static void alg_require(bool valid) { if (!valid) throw std::invalid_argument("invalid algorithm input"); }
static int alg_checked(int64_t v) { if (v < INT32_MIN || v > INT32_MAX) throw std::overflow_error("32-bit result overflow"); return (int)v; }
static int alg_add(int a,int b) { return alg_checked((int64_t)a+b); }
static int alg_sub(int a,int b) { return alg_checked((int64_t)a-b); }
static int alg_mul(int a,int b) { return alg_checked((int64_t)a*b); }
static size_t alg_size(int n) { alg_require(n>=0 && n<=4000000); return (size_t)n; }

int n_queens(int n) {
    alg_require(((n >= 0) && (n <= 1000000)));
    alg_require(((n >= 0) && (n <= 12)));
    std::vector<int> columns(alg_size(alg_add(n, 1)), 0);
    std::vector<int> rising(alg_size(alg_add(alg_mul(2, n), 1)), 0);
    std::vector<int> falling(alg_size(alg_add(alg_mul(2, n), 1)), 0);
    std::vector<int> placement(alg_size(alg_add(n, 1)), 0);
    int row = 0;
    int col = 0;
    int count = 0;
    if ((n == 0)) {
        return 1;
    }
    while ((row >= 0)) {
        col = placement[row];
        if ((col > 0)) {
            columns[alg_sub(col, 1)] = 0;
            rising[alg_sub(alg_add(row, col), 1)] = 0;
            falling[alg_add(alg_sub(row, col), n)] = 0;
        }
        while (((col < n) && ((columns[col] != 0) || (rising[alg_add(row, col)] != 0) || (falling[alg_sub(alg_add(alg_sub(row, col), n), 1)] != 0)))) {
            col = alg_add(col, 1);
        }
        if ((col == n)) {
            placement[row] = 0;
            row = alg_sub(row, 1);
        } else {
            placement[row] = alg_add(col, 1);
            columns[col] = 1;
            rising[alg_add(row, col)] = 1;
            falling[alg_sub(alg_add(alg_sub(row, col), n), 1)] = 1;
            if ((row == alg_sub(n, 1))) {
                count = alg_add(count, 1);
            } else {
                row = alg_add(row, 1);
                placement[row] = 0;
            }
        }
    }
    return count;
}

int main(void) {
    int n = 0;
    int result = n_queens(n);
    printf("%d\n", result);
    return 0;
}
Algorithms: Backtracking

N Queens Backtracking in C#

N-queens places one queen in each row with no shared columns or diagonals..

csharp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
// N Queens Backtracking in C#
//
// PURPOSE
// N-queens places one queen in each row with no shared columns or diagonals.
//
// METHOD
// Try columns row by row. Keep occupied-column and diagonal marker arrays for constant-time
// safety checks. Backtrack when no column remains and count complete placements.
//
// COMPLEXITY
// Exponential search, bounded by O(n!) candidate placements with additional column-scan
// overhead; O(n) auxiliary space.
//
// CONTRACT
// 0<=n<=12. Returns solution count. The empty board has one placement. This implementation
// avoids recursion.
// Public APIs infer array lengths except in C, which accepts pointer/length pairs. Vertex
// counts and matrix dimensions remain explicit. Arrays must contain signed 32-bit integers.
// Sorted order, correct tree topology, and caller-owned data-structure invariants are
// preconditions; checking them on every query would change query complexity.
// Scalar and shape checks are included. Value-range checks are included where needed to
// prevent invalid indexing or nontermination. Allocation sizes are capped at 4,000,000
// workspace entries. Arithmetic outside Python raises an error on 32-bit overflow; Python
// uses arbitrary precision. Native allocation failures propagate. In-place outputs may be
// partly updated after arithmetic failure. These are single-threaded, in-memory
// implementations.
//
// LANGUAGE DETAILS
// C# / .NET 10: ReadOnlySpan<int> inputs, Span<int> outputs, primitive workspace arrays,
// Array.BinarySearch for LIS, and checked integer arithmetic. Invalid arguments and overflow
// throw exceptions.
//
// EXAMPLE
// Expected return: 1.
// Run: Use Program.cs in a net10.0 console project; dotnet run.
// Tests for this exact source artifact run before publication. The accompanying source
// archive includes unit cases, runtime results, and source hashes.

class Program {
    static void alg_require(bool valid) { if (!valid) throw new ArgumentException("invalid algorithm input"); }
    static int alg_size(int n) { alg_require(n>=0 && n<=4000000); return n; }

    static int n_queens(int n) {
        checked {
            alg_require(((n >= 0) && (n <= 1000000)));
            alg_require(((n >= 0) && (n <= 12)));
            int[] columns = new int[alg_size((n + 1))];
            int[] rising = new int[alg_size(((2 * n) + 1))];
            int[] falling = new int[alg_size(((2 * n) + 1))];
            int[] placement = new int[alg_size((n + 1))];
            int row = 0;
            int col = 0;
            int count = 0;
            if ((n == 0)) {
                return 1;
            }
            while ((row >= 0)) {
                col = placement[row];
                if ((col > 0)) {
                    columns[(col - 1)] = 0;
                    rising[((row + col) - 1)] = 0;
                    falling[((row - col) + n)] = 0;
                }
                while (((col < n) && ((columns[col] != 0) || (rising[(row + col)] != 0) || (falling[(((row - col) + n) - 1)] != 0)))) {
                    col = (col + 1);
                }
                if ((col == n)) {
                    placement[row] = 0;
                    row = (row - 1);
                } else {
                    placement[row] = (col + 1);
                    columns[col] = 1;
                    rising[(row + col)] = 1;
                    falling[(((row - col) + n) - 1)] = 1;
                    if ((row == (n - 1))) {
                        count = (count + 1);
                    } else {
                        row = (row + 1);
                        placement[row] = 0;
                    }
                }
            }
            return count;
        }
    }

    static void Main() {
        int n = 0;
        int result = n_queens(n);
        Console.WriteLine(result);
    }
}
Algorithms: Backtracking

N Queens Backtracking in PHP

N-queens places one queen in each row with no shared columns or diagonals..

php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
// N Queens Backtracking in PHP
//
// PURPOSE
// N-queens places one queen in each row with no shared columns or diagonals.
//
// METHOD
// Try columns row by row. Keep occupied-column and diagonal marker arrays for constant-time
// safety checks. Backtrack when no column remains and count complete placements.
//
// COMPLEXITY
// Exponential search, bounded by O(n!) candidate placements with additional column-scan
// overhead; O(n) auxiliary space.
//
// CONTRACT
// 0<=n<=12. Returns solution count. The empty board has one placement. This implementation
// avoids recursion.
// Public APIs infer array lengths except in C, which accepts pointer/length pairs. Vertex
// counts and matrix dimensions remain explicit. Arrays must contain signed 32-bit integers.
// Sorted order, correct tree topology, and caller-owned data-structure invariants are
// preconditions; checking them on every query would change query complexity.
// Scalar and shape checks are included. Value-range checks are included where needed to
// prevent invalid indexing or nontermination. Allocation sizes are capped at 4,000,000
// workspace entries. Arithmetic outside Python raises an error on 32-bit overflow; Python
// uses arbitrary precision. Native allocation failures propagate. In-place outputs may be
// partly updated after arithmetic failure. These are single-threaded, in-memory
// implementations.
//
// LANGUAGE DETAILS
// PHP 8.1+ on a 64-bit runtime: packed arrays, strict scalar types, read-only inputs by
// value using copy-on-write, references only for outputs, and checked 32-bit arithmetic.
// Invalid input and overflow throw exceptions. PHP arrays have higher overhead than native
// packed buffers in C/Java.
//
// EXAMPLE
// Expected return: 1.
// Run: php example.php
// Tests for this exact source artifact run before publication. The accompanying source
// archive includes unit cases, runtime results, and source hashes.

function alg_require(bool $valid): void { if (!$valid) throw new InvalidArgumentException("invalid algorithm input"); }
function alg_checked(int|float $v): int { if ($v < -2147483648 || $v > 2147483647 || floor($v) != $v) throw new OverflowException("32-bit result overflow"); return (int)$v; }
function alg_add(int $a,int $b): int { return alg_checked($a+$b); }
function alg_sub(int $a,int $b): int { return alg_checked($a-$b); }
function alg_mul(int $a,int $b): int { return alg_checked($a*$b); }
function alg_size(int $n): int { alg_require($n>=0 && $n<=4000000); return $n; }

function n_queens(int $n): int {
    alg_require((($n >= 0) && ($n <= 1000000)));
    alg_require((($n >= 0) && ($n <= 12)));
    $columns = array_fill(0, alg_size(alg_add($n, 1)), 0);
    $rising = array_fill(0, alg_size(alg_add(alg_mul(2, $n), 1)), 0);
    $falling = array_fill(0, alg_size(alg_add(alg_mul(2, $n), 1)), 0);
    $placement = array_fill(0, alg_size(alg_add($n, 1)), 0);
    $row = 0;
    $col = 0;
    $count = 0;
    if (($n == 0)) {
        return 1;
    }
    while (($row >= 0)) {
        $col = $placement[$row];
        if (($col > 0)) {
            $columns[alg_sub($col, 1)] = 0;
            $rising[alg_sub(alg_add($row, $col), 1)] = 0;
            $falling[alg_add(alg_sub($row, $col), $n)] = 0;
        }
        while ((($col < $n) && (($columns[$col] != 0) || ($rising[alg_add($row, $col)] != 0) || ($falling[alg_sub(alg_add(alg_sub($row, $col), $n), 1)] != 0)))) {
            $col = alg_add($col, 1);
        }
        if (($col == $n)) {
            $placement[$row] = 0;
            $row = alg_sub($row, 1);
        } else {
            $placement[$row] = alg_add($col, 1);
            $columns[$col] = 1;
            $rising[alg_add($row, $col)] = 1;
            $falling[alg_sub(alg_add(alg_sub($row, $col), $n), 1)] = 1;
            if (($row == alg_sub($n, 1))) {
                $count = alg_add($count, 1);
            } else {
                $row = alg_add($row, 1);
                $placement[$row] = 0;
            }
        }
    }
    return $count;
}

$n = 0;
$result = n_queens($n);
echo $result, PHP_EOL;