Cod sursa(job #418540)

Utilizator DITzoneCAdrian Diaconu DITzoneC Data 15 martie 2010 23:38:09
Problema Copii Scor Ascuns
Compilator cpp Status done
Runda Marime 1.21 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][nmax];
char s[nmax][nmax];

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

void doit(int i)
{
  if(i == n)
  {
    if(test())
      sol++;
    return;
  }
  int j, old = 0, t;
  FOR(j, 0, k + 1)
  {
    if(i)
      FOR(t, 0, n)
        aux[i][t] = aux[i - 1][t];
    else
      memset(aux[0], 0, sizeof(aux[0]));
    col[i] = j;
    FOR(t, 0, i)
    {
      if(s[i][t] == '1')
        aux[i][j] |= pt(col[t]);
      if(s[t][i] == '1')
        aux[i][col[t]] |= pt(j);
    }
    aux[i][j] |= pt(j);
    if(j == k)
    {
      k++;
      doit(i + 1);
      k--;
    }
    else
      doit(i + 1);
  }
}

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;
}