aboutsummaryrefslogtreecommitdiff
path: root/tests/sysy_lvX/093_n_queens.sy
blob: 5a5c0860255352bd2aac7391ed56104ef000ccc0 (plain)
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
int ans[50], sum = 0, n;

int row[50], line1[50], line2[100];

void printans()
{
    sum = sum + 1;
    int i = 1;
    while (i <= n) {
        putint(ans[i]);
        if (i == n) {
            putch(10);
            return;
        } else
            putch(32);
        i = i + 1;
    }
}

void f(int step)
{
    int i = 1;
    while (i <= n) {
        if (row[i] != 1 && line1[step + i] == 0 && !line2[n + step - i]) {
            ans[step] = i;
            if (step == n)
                printans();
            row[i] = 1;
            line1[step + i] = 1;
            line2[n + step - i] = 1;
            f(step + 1);
            row[i] = 0;
            line1[step + i] = 0;
            line2[n + step - i] = 0;
        }
        i = i + 1;
    }
}

int main()
{
    int N = getint();
    while (N > 0) {
        n = getint();
        f(1);
        N = N - 1;
    }
    return sum;
}