61分,最后三个点wa了求调

P1219 [USACO1.5] 八皇后 Checker Challenge

TreeHole_qwq @ 2024-01-31 20:46:05

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<ctime>
using namespace std;
int n,a[21],sum;
bool check[3][21];
void dfs(int i)
{
    if(i>n)
    {
        sum++;
        if(sum<=3)
        {
            for(int j=1;j<=n;j++) cout<<a[j]<<" ";
            cout<<endl;
        }
        return;
    }
    for(int j=1;j<=n;j++)
    {
        if(!check[0][j]&&!check[1][i+j]&&!check[2][i-j+n-1])
        {
            check[0][j]=1;
            check[1][i+j]=1;
            check[2][i-j+n-1]=1;
            a[i]=j;
            dfs(i+1);
            check[0][j]=0;
            check[1][i+j]=0;
            check[2][i-j+n-1]=0;
        }
    }
}
int main()
{
    cin>>n;
    dfs(1);
    cout<<sum;
}

都是sum的值错了,为什么啊qaq


by hema5177 @ 2024-01-31 20:57:31

@TreeHole_qwq check的数组开大一点吧?毕竟当i和j都等于n的最大值时会超下标


by TreeHole_qwq @ 2024-01-31 20:59:30

@hema5177 谢谢!a了!


by hema5177 @ 2024-01-31 20:59:56

@TreeHole_qwq 不用谢qwq


|