Cod sursa(job #1380778)

Utilizator gabi.cristacheGabi Cristache gabi.cristache Data 8 martie 2015 10:50:17
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <iostream>
#include <vector>
#include <fstream>

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

std::vector<int> v;
int N;

bool permutareValida(int k) {
	for (int i = 0; i < k; ++i)
		if (v[i] == v[k])
			return false;
	return true;
}

void bk(int k) {
	for (int i = 1; i <= N; ++i) {
		v[k] = i;

		if (permutareValida(k)) {
			if (k == N - 1) {
				for (int j = 0; j <= k; ++j) {
					fout << v[j] << ' ';
				}
				fout << '\n';
			} else {
				bk(k + 1);
			}
		}
	}	
}

int main() {
	fin >> N;

	v.resize(N);

	bk(0);

	return 0;
}