大佬帮忙看看哪错了

P1219 [USACO1.5] 八皇后 Checker Challenge

gogei @ 2023-02-24 19:55:51

#include <bits/stdc++.h>
using namespace std;
int n, a[100], ans;
bool b[100], c[100], d[100];
void print()
{
    if (ans <= 2)
    {
        for (int i = 1; i <= n; i++)
            cout << a[i] << " ";
        cout << "\n";
    }
    ans++;
}
void dfs(int x)
{
    cout << x << " ";
    if (x > n)
    {
        print();
        return;
    }
    else
    {
        for (int i = 1; i <= n; i++)
        {
            if ((!b[i]) && (!c[x + i]) && (!d[x - i + n]))
            {

                a[x] = i;
                b[x] = 1;
                c[x + i] = 1;
                d[x - i + n] = 1;
                dfs(x + 1);
                b[x] = 0;
                c[x + i] = 0;
                d[x - i + n] = 0;
            }
        }
    }
}
int main()
{
    cin >> n;
    dfs(1);
    cout << ans;
    return 0;
}

by CultReborn @ 2023-02-25 15:18:30

27~37判断条件炸了

#include <bits/stdc++.h>
using namespace std;
int n, a[100], ans;
bool b[100], c[100], d[100];
void print()
{
    if (ans <= 2)
    {
        for (int i = 1; i <= n; i++)
            cout << a[i] << " ";
        cout << endl;
    }
    ans++;
}
void dfs(int x)
{
//    cout << x << " ";
    if (x > n)
    {
        print();
        return;
    }
    else
    {
        for (int i = 1; i <= n; i++)
        {
            if ((!b[i]) && (!c[x + i]) && (!d[i - x + n]))
            {

                a[x] = i;
                b[i] = 1;
                c[x + i] = 1;
                d[i - x + n] = 1;
                dfs(x + 1);
                b[i] = 0;
                c[x + i] = 0;
                d[i - x + n] = 0;
            }
        }
    }
}
int main()
{
    cin >> n;
    dfs(1);
    cout << ans;
    return 0;
}

by CultReborn @ 2023-02-25 15:44:18

@liunawq


|