Cod sursa(job #1702122)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 14 mai 2016 16:03:25
Problema Copii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <bits/stdc++.h>

const int DIM = 1 << 4;
using namespace std;

int V[DIM], W[DIM], T[DIM], N, answer; char S[DIM];

inline bool solve( int val, int pos ) {
    if( pos == 1 )
        return false;

    for( int i =   0  ; i < pos; i ++ ) {
    for( int j = i + 1; j < pos; j ++ ) {
        if( ( W[i] & T[j] ) == 0 || ( W[j] & T[i] ) == 0 )
            return false;
    }}

    return true;
}

void back_track( int val, int pos ) {
    if( val == N ) {
        answer += solve( val, pos );
        return;
    }

    for( int i = 0; i < pos; i ++ ) {
        int last = W[i];
        W[i] = ( W[i] | V[val] );
        T[i] += (1 << val);

        back_track( val + 1, pos );

        W[i] = last;
        T[i] -= (1 << val);
    }

    if( T[pos - 1] == 0 )
        return;

    int last = W[pos];
    W[pos] += V[val];
    T[pos] += (1 << val);

    back_track( val + 1, pos + 1 );

    W[pos] = last;
    T[pos] -= (1 << val);

    return;
}

int main() {

    FILE *input_file  = fopen( "copii.in" , "r" );
    FILE *output_file = fopen( "copii.out", "w" );

    fscanf( input_file, "%d", &N );

    for( int i = 0; i < N; i ++ ) {
        fscanf( input_file, "%s", S );

        for( int j = 0; j < N; j ++ )
            V[i] += (1 << j) * (S[j] - '0');
    }

    back_track( 0, 1 );
    fprintf( output_file, "%d\n", answer );

    return 0;
}