Cod sursa(job #1857727)

Utilizator roxannemafteiuMafteiu-Scai Roxana roxannemafteiu Data 26 ianuarie 2017 16:14:40
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

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

short n;
vector <short> st;

bool isValid(short level) {
   for (short i = 0; i < level; ++i) {
        if (st[i] == st[level]) {
            return false;
        }
   }

   return true;
}

void write(short level) {
    for (const auto &i : st) {
        fout << i << ' ';
    }

    fout << '\n';
}

void backt(short level) {
    for (short i = 1; i <= n; ++i) {
        st[level] = i;
        if (isValid(level)) {
            if (level == n - 1) {
                write(level);
            } else {
                backt(level + 1);
            }
        }
    }
}

int main() {
    fin >> n;
    st.resize(n);

    backt(0);

    return 0;
}