Cod sursa(job #2152636)

Utilizator EclipseTepes Alexandru Eclipse Data 5 martie 2018 18:21:24
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>

using namespace std;

unsigned short int n, x[9];

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

bool Candidate(unsigned short int y) {
    for (unsigned short int i = 1; i < y; i++) if (x[i] == x[y]) return false;
    return true;
}

void BackTrack(unsigned short int y) {
    unsigned short int i, j;
    for (i = 1; i <= n; i++) {
        x[y] = i;
        if (Candidate(y)) {
            if (y == n) {
                for (j = 1; j <= n; j++) {
                    fout << x[j] << " ";
                }
                fout << "\n";
            } else BackTrack(y + 1);
        }
    }
}

int main()
{
    fin >> n;
    BackTrack(1);
    return 0;
}