Cod sursa(job #3245110)

Utilizator calinulCalin Cernat calinul Data 27 septembrie 2024 15:59:21
Problema Problema Damelor Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <stdio.h>

#define MAX_N 13
#define SHIFT MAX_N

char fl[MAX_N], fp[MAX_N + SHIFT], fs[MAX_N + SHIFT], v[MAX_N], n, fa = 0;

FILE *fin, *fout;

int sol( int lin ) {
  int col, rez = 0;

  if ( lin == n + 1 ) {
    if ( !fa ) {
      fa = 1;
      for ( col = 1; col <= n; col++ )
        fprintf( fout, "%d ", v[col] );
    }
    return 1;
  }

  for ( col = 1; col <= n; col++ ) {
    if ( !fl[col] && !fp[col - lin + SHIFT] && !fs[col + lin - 1] ) {
      fl[col] = fp[col - lin + SHIFT] = fs[col + lin - 1] = 1;
      v[lin] = col;
      rez += sol( lin + 1 );
      fl[col] = fp[col - lin + SHIFT] = fs[col + lin - 1] = 0;
    }
  }

  return rez;
}

int main() {
  int rez, i;

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

  fout = fopen( "damesah.out", "w" );
  rez = sol( 1 );
  fprintf( fout, "\n%d\n", rez );
  fclose( fout );

  return 0;
}