24分,其它全WA,大佬,求调

P1219 [USACO1.5] 八皇后 Checker Challenge

tall_buildin @ 2024-07-16 11:34:55

#include <bits/stdc++.h>
using namespace std;
int n;
int a[15];
bool d1[15],d2[15],d3[15];
long long ans = 0;
void dfs(int x){
    if (x>n){
        ans++;
        if (ans<=3){
            for (int i = 1;i<=n;i++)
                cout<<a[i]<<" ";
            cout<<endl; 
        }
        return;
    }
    for (int i = 1;i<=n;i++){

        if(d1[i] == 1&&d2[x+i-1] == 1&&d3[x-i+n]){
//          cout<<"第"<<x<<"个皇后,i="<<i<<" 第"<<i<<"列为"<<d1[i]<<" 第"<<x+i-1<<"号左对角线为"<<d2[x+i-1]<<" 第"<<x-i+n<<"号右对角线为"<<d3[x-i+n]<<endl; 
//          cout<<"可选"<<endl;   
            a[x] = i;
            d1[i] = 0;
            d2[x+i-1] = 0;
            d3[x-i+n] = 0;
            dfs(x+1);
            d1[i] = 1;
            d2[x+i-1] = 1;
            d3[x-i+n] = 1;
        }
    }

}   
int main(){
    memset(d1,1,sizeof(d1));
    memset(d2,1,sizeof(d2));
    memset(d3,1,sizeof(d3));
    cin>>n;
    dfs(1);
    cout<<ans;

    return 0;
} 

by 奈芙蓮 @ 2024-07-16 11:37:23

印象中不行

memset(d1,1,sizeof(d1));
memset(d2,1,sizeof(d2));
memset(d3,1,sizeof(d3));

by 奈芙蓮 @ 2024-07-16 11:39:23

https://www.luogu.com/article/q9p8e1bc


by tall_buildin @ 2024-07-16 11:43:39

**改完后成36分了**
#include <bits/stdc++.h>
using namespace std;
int n;
int a[15];
bool d1[15],d2[15],d3[15];
long long ans = 0;
void dfs(int x){
    if (x>n){
        ans++;
        if (ans<=3){
            for (int i = 1;i<=n;i++)
                cout<<a[i]<<" ";
            cout<<endl; 
        }
        return;
    }
    for (int i = 1;i<=n;i++){

        if(d1[i] == 0&&d2[x+i-1] == 0&&d3[x-i+n] == 0){
//          cout<<"第"<<x<<"个皇后,i="<<i<<" 第"<<i<<"列为"<<d1[i]<<" 第"<<x+i-1<<"号左对角线为"<<d2[x+i-1]<<" 第"<<x-i+n<<"号右对角线为"<<d3[x-i+n]<<endl; 
//          cout<<"可选"<<endl;   
            a[x] = i;
            d1[i] = 1;
            d2[x+i-1] = 1;
            d3[x-i+n] = 1;
            dfs(x+1);
            d1[i] = 0;
            d2[x+i-1] = 0;
            d3[x-i+n] = 0;
        }
    }

}   
int main(){
//  memset(d1,0,sizeof(d1));
//  memset(d2,0,sizeof(d2));
//  memset(d3,0,sizeof(d3));
    cin>>n;
    dfs(1);
    cout<<ans;

    return 0;
} 

by tall_buildin @ 2024-07-16 11:45:35

@stemdarrenyang 改完后成36分了

#include <bits/stdc++.h>
using namespace std;
int n;
int a[15];
bool d1[15],d2[15],d3[15];
long long ans = 0;
void dfs(int x){
    if (x>n){
        ans++;
        if (ans<=3){
            for (int i = 1;i<=n;i++)
                cout<<a[i]<<" ";
            cout<<endl; 
        }
        return;
    }
    for (int i = 1;i<=n;i++){

        if(d1[i] == 0&&d2[x+i-1] == 0&&d3[x-i+n] == 0){
//          cout<<"第"<<x<<"个皇后,i="<<i<<" 第"<<i<<"列为"<<d1[i]<<" 第"<<x+i-1<<"号左对角线为"<<d2[x+i-1]<<" 第"<<x-i+n<<"号右对角线为"<<d3[x-i+n]<<endl; 
//          cout<<"可选"<<endl;   
            a[x] = i;
            d1[i] = 1;
            d2[x+i-1] = 1;
            d3[x-i+n] = 1;
            dfs(x+1);
            d1[i] = 0;
            d2[x+i-1] = 0;
            d3[x-i+n] = 0;
        }
    }

}   
int main(){
//  memset(d1,0,sizeof(d1));
//  memset(d2,0,sizeof(d2));
//  memset(d3,0,sizeof(d3));
    cin>>n;
    dfs(1);
    cout<<ans;

    return 0;
} 

by 奈芙蓮 @ 2024-07-16 11:54:05

bool d1[15],d2[15],d3[15];

开大一点试试


by tall_buildin @ 2024-07-16 14:31:04

谢谢大佬,AC了,此贴结


|