Cod sursa(job #1086238)

Utilizator Teodor94Teodor Plop Teodor94 Data 17 ianuarie 2014 21:36:44
Problema Problema Damelor Scor Ascuns
Compilator cpp Status done
Runda Marime 0.98 kb
#include <cstdio>

int regine[14];
int col[14];
int principal[27];
int secundar[27];
int np;

void bkt( FILE *fout, int l, int n ) {
    int c;
    if ( l == n ) {
            if ( ++np < 2 ) {
                for ( c = 0; c < n; c++ )
                    fprintf( fout, "%d ", regine[c] + 1 );
                fprintf( fout, "\n" );
            }
    } else {
        for ( c = 0; c < n; c++ ) {
            if ( !col[c] && !principal[c-l+n-1] && !secundar[l+c] ) {
                regine[l] = c;
                col[c] = principal[c-l+n-1] = secundar[l+c] = 1;
                bkt( fout, l + 1, n );
                col[c] = principal[c-l+n-1] = secundar[l+c] = 0;
            }
        }
    }
}

int main () {
    FILE *fin, *fout;

    fin = fopen( "damesah.in", "r" );
    int n;
    fscanf( fin, "%d", &n );
    fclose( fin );

    fout = fopen( "damesah.out", "w" );
    np = 0;
    bkt( fout, 0, n );
    fprintf( fout, "%d\n", np );
    fclose( fout );
}