Cod sursa(job #3332652)

Utilizator DariusJohnDarius Dumitrescu DariusJohn Data 8 ianuarie 2026 11:51:59
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <bits/stdc++.h>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int n, ans;
bool ok;
bool valid(vector<int> &perm) {
  int index = 0;
  for (int i = perm.size() - 2; i >= 0; i--) {
    index++;
    if (perm[i] + index == perm.back() || perm[i] - index == perm.back() ||
        perm[i] == perm.back())
      return 0;
  }
  return 1;
}

void print(vector<int> &perm) {
  if (!ok) {
    ok ^= 1;
    for (auto it : perm)
      fout << it << " ";
    fout << "\n";
  }
  ans++;
}

void gen(vector<int> &perm) {
  if (perm.size() < n) {
    for (int i = 1; i <= n; i++) {
      perm.push_back(i);
      if (valid(perm))
        gen(perm);
      perm.pop_back();
    }
  } else
    print(perm);
}
int main() {
  fin >> n;
  vector<int> perm;
  gen(perm);
  fout << ans;
  return 0;
}