Cod sursa(job #1449892)

Utilizator cristina_borzaCristina Borza cristina_borza Data 10 iunie 2015 21:08:53
Problema Copii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream f("copii.in");
ofstream g("copii.out");

int n , sol ;
int mat[20][20] , aux[20][20] , v[20] , c[20];

void bkt(int poz);
bool verif();

int main(){
    char ch ;

    f >> n ;
    for(int i = 1 ; i <= n ; ++i){
        for(int j = 1 ; j <= n ; ++j){
            f >> ch ;
            mat[i][j] = ch - '0';
        }
    }

    bkt(1);

    g << sol ;

    return 0;
}

void bkt(int poz){
    if(poz > n){
        sol += verif();
        return ;
    }
    for(int i = 1 ; i <= n ; ++i){
        if(c[i - 1] != 0 || i == 1){
            v[poz] = i ;
            ++c[i] ;
            bkt(poz + 1);
            --c[i];
        }
    }
}

bool verif(){
    int sm = 0 ;
    for(int i = 1 ; i <= n ; ++i){
        sm = max(sm , v[i]);
    }
    if(sm == 1){
        return 0 ;
    }
    for(int i = 1 ; i <= n ; ++i){
        for(int j = 1 ; j <= n ; ++j){
            aux[i][j] = 0 ;
            if(mat[i][j] == 1 ){
                aux[v[i]][v[j]] = 1;
            }
        }
    }
    for(int i = 1 ; i <= sm ; ++i){
        for(int j = 1 ; j <= sm ; ++j){
            if(aux[i][j] == 0 && i != j){
                return 0;
            }
        }
    }
    return 1;
}