87分#8TLE求助!~

P1219 [USACO1.5] 八皇后 Checker Challenge

Weizhuo_Zhao @ 2023-07-28 14:36:21

#include<iostream>
using namespace std;
int a[15],k,c=0;
long long ans;
bool q(int n){
    for(int i=1;i<n;i++)
        if(a[i]==a[n]||i-a[i]==n-a[n]||i+a[i]==n+a[n])
            return 0;
    return 1;
}
void dfs(int n){
    if(n==k+1){
        ans++;
        for(int i=1;i<=k;i++){
            if(c>=3){
                return;
            }
            printf("%d ",a[i]);
        }
        c++;
        printf("\n");
    }
    else{
        for(int i=1;i<=k;i++){
            a[n]=i;
            if(q(n)){
                dfs(n+1);
            }
        }
    }
}
int main(){
    cin>>k;
    dfs(1);
    cout<<ans;
    return 0;
}

by Zz__Cc @ 2023-07-28 15:11:08

@Weizhuo_Zhao 吸个氧就过了


by Weizhuo_Zhao @ 2023-07-28 15:50:48

@Zz__Cc ???


by Zz__Cc @ 2023-07-28 16:01:11

@Weizhuo_Zhao ???


by Zz__Cc @ 2023-07-28 16:01:22

@Weizhuo_Zhao 就是开O2


by Weizhuo_Zhao @ 2023-07-29 08:21:32

@Zz__Cc ???化学名???


by Zz__Cc @ 2023-07-29 10:04:05

@Weizhuo_Zhao 大哥,O2优化


by Weizhuo_Zhao @ 2023-07-29 11:15:49

@Zz__Cc 哦哦哦,脑子傻了!


by Weizhuo_Zhao @ 2023-07-29 22:32:39

@Zz__Cc AC啦!谢啦!!☆⌒(*^-゜)v,为什么发那么晚:因为在我发:哦哦哦,脑子傻了! 之后,教练一直将CFP-JAK的纸面的考点,不然开电脑!


|