Pagini recente » Cod sursa (job #727768) | Istoria paginii runda/summer_camp_3 | simulare.wellspecial2 | Autentificare | Cod sursa (job #2006749)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("permutari.in");
ofstream g ("permutari.out");
int *x, nrsol = 0;
int valid (int x[], int k)
{
for(int i = 1; i < k; i++)
if(x[k] == x[i] )
return 0;
return 1;
}
void afis (int x[], int n)
{
nrsol++;
for (int i = 1; i <= n; i++)
g << x[i] << ' ';
g << '\n';
}
void backt (int n)
{
int k = 1;
x[1] = 0;
while (k > 0)
if (x[k] < n)
{
x[k]++;
//ap[x[k]]++;
if (valid (x, k))
{
if (k == n)
{
afis (x, n);
//ap[x[k]]--;
k--; //se poate forta revenirea
}
else
x[++k] = 0;
}
}
else
k--;
}
int main()
{
int N;
f >> N;
x = new int [N + 1];
backt (N);
return 0;
}