蒟蒻求救,为什么只输出0 QwQ

P1219 [USACO1.5] 八皇后 Checker Challenge

woshinidegou @ 2023-12-10 21:37:51

#include<bits/stdc++.h>
using ll=long long;
using namespace std;
ll n,a[15],ans = 0;
bool vis1[15],vis2[45],vis3[45];
void dfs(ll x) {
    if(x==n){
        ans++;
        if(ans<=3){
            for (ll i= 1;i<=n;i++){
                cout<<a[i]<<"\n"[i==n];
            }
        }return;
    }for(ll i=1;i<=n;i++) {
        if(!vis1[i] && !vis2[x-i+20] && !vis3[x+i]){
            vis1[i]=1;
            vis2[x-i+20]=1;
            vis3[x+i] = 1;
            a[x]=i;
            dfs(x+1);
            vis1[i]=0;
            vis2[x-i+20]=0;
            vis3[x+i]=0;
            a[x]=0;
        }
    }
}int main() {
    cin>>n;
    dfs(n);
    cout<<ans;
    return 0;
}

by Yuiu2233 @ 2023-12-14 13:50:16

dfs(n)... 这不直接出界了?


|