Pagini recente » Cod sursa (job #1746869) | Cod sursa (job #2458246) | Cod sursa (job #1576449) | Cod sursa (job #271492) | Cod sursa (job #943487)
Cod sursa(job #943487)
/// Craciun Catalin (c)
/// Backtracking
/// Generare permutari
#include <fstream>
#include <iostream>
using namespace std;
ifstream f("permutari.in");
ofstream g("permutari.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;
}