Pagini recente » Cod sursa (job #346695) | Cod sursa (job #2484572) | Cod sursa (job #3148358) | Cod sursa (job #3153685) | Cod sursa (job #2152636)
#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;
}