Cod sursa(job #418526)

Utilizator DITzoneCAdrian Diaconu DITzoneC Data 15 martie 2010 23:07:08
Problema Copii Scor Ascuns
Compilator cpp Status done
Runda Marime 0.99 kb
#include <cstdio>
#include <cassert>
#include <cstring>

#define nmax 16
#define FOR(i, s, d) for(i = (s); i < (d); ++i)
#define pt(i) (1 << (i))

int n, sol, col[nmax], k, aux[nmax];
char s[nmax][nmax];

int test()
{
  int i, j;
  memset(aux, 0, sizeof(aux));
  FOR(i, 0, n)
    FOR(j, 0, n)
      if(i == j || s[i][j] == '1')
        aux[col[i]] |= pt(col[j]);
  FOR(i, 0, k)
    if(aux[i] != pt(k) - 1)
      return 0;
  return 1;
}

void doit(int i)
{
  if(i == n)
  {
    if(test())
      sol++;
    return;
  }
  int j;
  FOR(j, 0, k)
  {
    col[i] = j;
    doit(i + 1);
  }
  col[i] = k++;
  doit(i + 1);
  k--;
}

int main()
{
  FILE *f = fopen("copii.in", "r");
  int i, j;
  assert(f);
  assert(fscanf(f, "%d", &n) == 1);
  assert(1 <= n && n <= 10);
  FOR(i, 0, n)
  {
    assert(fscanf(f, "%s", s[i]) == 1);
    assert(!s[i][n]);
    FOR(j, 0, n)
      assert(s[i][j] == '0' || s[i][j] == '1');
  }
  fclose(f);
  doit(0);
  freopen("copii.out", "w", stdout);
  printf("%d\n", sol - 1);
  return 0;
}