usaco checker

/*
ID: ljracm1
LANG: C++
PROB: checker
*/
#include
#define T

int n,t;
int col[15];
int dig1[40],dig2[40];
int row[15];
void dfs(int d){
    if(d>n){
        t++;
        if(t<=3){
            for(int i=1; i<=n; i++)
                printf("%d%c",row[i],i==n?'\n':' ');
        }
    }
    for(int i=1; i<=n; i++){
        if(!col[i]&&!dig1[i+d]&&!dig2[d+(n+1-i)]){
             col[i]=1;
             dig1[i+d]=1;
             dig2[d+(n+1-i)]=1;
             row[d]=i;
             dfs(d+1);
             col[i]=0;
             dig1[i+d]=0;
             dig2[d+(n+1-i)]=0;
        }
    }
}

int main(){
    #ifdef T
        freopen("checker.in","r",stdin);
        freopen("checker.out","w",stdout);
    #endif
    scanf("%d",&n);
    t=0;
    dfs(1);
    printf("%d\n",t);
    return 0;
}

你可能感兴趣的:(usaco checker)