求助!代码总是调不对

P1219 [USACO1.5] 八皇后 Checker Challenge

__hqt__ @ 2023-11-07 20:13:36

求调
题目:P1219

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
int n,a[101],b[101],c[101],ans[101],m=0;
void A(int h)
{
    if(h>n)
    {
        m++;
        if(m<=3)
        {
            for(int j=1;j<=n;j++)
            {
                cout<<ans[j]<<' ';
            }
            cout<<endl;
        }
        return;
    }
    for(int i=1;i<=n;i++)
    {
        if(a[i]==0 && b[(h-i+n)%n]==0 && c[(h+i+n)%n]==0)
        {
            a[i]=1;
            b[(h-i+n)%n]=1;
            c[(h+i+n)%n]=1;
            ans[h]=i;
            A(h+1);
            a[i]=0;
            b[(h-i+n)%n]=0;
            c[(h+i+n)%n]=0;
        }
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    A(1);
    cout<<m<<endl;
}

by wanglexi @ 2023-11-09 17:15:34

if(a[i]==0 && b[(h-i+n)%n]==0 && c[(h+i+n)%n]==0)

\%n 干啥

$\textcolor{green}{\texttt{AC}}$ 代码: ```cpp #include<bits/stdc++.h> using namespace std; #define endl '\n' int n,a[101],b[101],c[101],ans[101],m=0; void A(int h) { if(h>n) { m++; if(m<=3) { for(int j=1;j<=n;j++) { cout<<ans[j]<<' '; } cout<<endl; } return; } for(int i=1;i<=n;i++) { if(a[i]==0 && b[h-i+n]==0 && c[h+i+n]==0) { a[i]=1; b[h-i+n]=1; c[h+i+n]=1; ans[h]=i; A(h+1); a[i]=0; b[h-i+n]=0; c[h+i+n]=0; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; A(1); cout<<m<<endl; } ```

by wanglexi @ 2023-11-09 17:15:56

@hqt_


by __hqt__ @ 2023-11-10 18:58:01

谢谢大佬,此贴已结束


|