Cod sursa(job #2416894)

Utilizator avtobusAvtobus avtobus Data 28 aprilie 2019 15:08:50
Problema Generare de permutari Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
// Generarea de permutari cu backtracking (cu un array care tine minte cifrele utilizate)
#include <stdio.h>
#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; i++)
#define REP(i,a,b) for(int i = a; i < b; i++)

using namespace std;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;

int N, d[10];
char used[10];

void write();
void back(int lvl);

int main(void) {
  freopen("permutari.in", "r", stdin);
  freopen("permutari.out", "w", stdout);
  cin >> N;

  back(0);

  return 0;
}

void write() {
  rep(i, N) { cout << d[i] << ' '; }
  cout << '\n';
}

void back(int lvl) {
  if (lvl == N) {
    write();
    return;
  }

  rep(i, N) {
    if (used[i]) { continue; }
    used[i] = 1; d[lvl] = i+1;
    back(lvl + 1);
    d[lvl] = 0; used[i] = 0;
  }
}