全部评论 1

  • 八皇后

    #include<bits/stdc++.h>
    using namespace std;
    
    using i64 = long long;
    int a[50];
    int b[50];
    int c[50];
    int d[50];
    int cnt = 0;
    
    void dfs(int x, int n) {
        if (x > n) {
            if (cnt < 3) {
                for (int i = 1; i <= n; i++)
                    cout << a[i] << " ";
                cout << endl;
            }
            cnt++;
        }
        for (int i = 1; i <= n; i++) {
            if (!b[i] && !c[x + i] && !d[n - x + i]) {
                a[x] = i;
                b[i] = 1;
                c[x + i] = 1;
                d[n - x + i] = 1;
                dfs(x + 1, n);
                b[i] = 0;
                c[x + i] = 0;
                d[n - x + i] = 0;
            }
        }
    }
    
    int main() {
        int n;
        cin >> n;
        dfs(1, n);
        cout <<cnt <<endl;
    }
    

    12小时前 来自 浙江

    0
首页