Cod sursa(job #1644035)

Utilizator justsomedudePalade Thomas-Emanuel justsomedude Data 9 martie 2016 21:18:50
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include<iostream>
#include<fstream>

using namespace std;

ifstream fin  ("damesah.in");
ofstream fout ("damesah.out");

int st[14], v[14], k, N;

inline int Modul(int x1, int x2)
{
   if (x1 - x2 >= 0) return (x1 - x2);
   else             return (x2 - x1);
}

inline bool Valid(int i, int top)
{
  int j;
  if (v[i] == 1) return 0;
  for (j=1; j<top; j++)
    if (Modul(top, j) == Modul(i, st[j]))
       return 0;
  return 1;
}

void Afisare()
{
   if (k == 0)
   {
     for (int i=1; i<=N; i++)
         fout << st[i] << " ";
        fout << "\n";
     k++;
   }
   else k++;
}

void Back(int top)
{
  int i;
  if (top == N+1) Afisare();
  else
   for (i=1; i<=N; i++)
    {
      if (Valid(i, top) == 1)
     {
        st[top] = i;
        v[i] = 1;
        Back(top+1);
        v[i] = 0;
     }
    }
}

int main ()
{
  fin >> N;
  Back(1);
  fout << k << "\n";
  fin.close();
  fout.close();
  return 0;
}