Pagini recente » Cod sursa (job #1859907) | Cod sursa (job #1309723) | Cod sursa (job #2827301) | Cod sursa (job #515461) | Cod sursa (job #943486)
Cod sursa(job #943486)
/// Craciun Catalin (c)
/// Backtracking
/// Generare permutari
#include <fstream>
#include <iostream>
using namespace std;
ifstream f("perm.in");
ofstream g("perm.out");
short int n;
short int V[101];
void afisare(){
for (int i=1;i<=n;i++)
g<<V[i]<<" ";
g<<endl;
}
bool posibil(short int p){
bool ok=1;
for (int i=1;i<p;i++){
if (i!=p)
if (V[i]==V[p])
ok=0;
}
return ok;
}
void bkt(){
short int k;
bool ok;
k=1;
V[k]=0;
while (k>0){
ok=0;
while (V[k]<n&&!ok){
V[k]++;
ok=posibil(k);
}
if (!ok)
k--;
else{
if (k==n)
afisare();
else{
k++;
V[k]=0;
}
}
}
}
int main(){
f>>n;
bkt();
return 0;
}