Pagini recente » Cod sursa (job #26552) | Cod sursa (job #1348289) | Cod sursa (job #2719486) | Cod sursa (job #2461467) | Cod sursa (job #2924327)
#include <fstream>
//#include <iostream>
#include <vector>
using namespace std;
/*
n=3
pos=1 : 1
pos=2 : 1 2
pos=3 : 1 2 3
pos=4 : afisare 1 2 3
pos=3 : 1 2 - nu mai am ce sa gasesc dupa 3 -> ma intorc
pos=2 : 1 3
pos=3 : 1 3 2
pos=4 : afiseaza 1 3 2
pos=3 : 1 3 - nu mai am ce sa gasesc dupa 2 -> ma intorc
pos=2 : 1 - nu mai am ce sa gasesc dupa 3 -> ma intorc
pos=1 : 2
...
*/
/*
n=4
pos=1 : 1
pos=2 : 1 2
pos=3 : 1 2 3
pos=4 : 1 2 3 4
pos=5 : afisare
pos=4 : 1 2 3 _ -> da false la folosit[4] si iese din for
pos=3 : 1 2 4 -> da false la folosit[3] si merge cu i=4
pos=4 : 1 2 4 3
pos=5 : afisare
pos=4 : 1 2 4 _ -> da false la folosit[3] si iese din for
pos=3 : 1 2 _ -> da false la folosit[4] si iese din for
pos=2 : 1 3 -> da false la folosit[2] si merge cu i=3
pos=3 : 1 3 2
pos=4 : 1 3 2 4
pos=5 : afisare
...
*/
//ifstream cin("input"); ofstream cout("output");
ifstream cin("permutari.in"); ofstream cout("permutari.out");
vector < bool > folosit;
vector < int > permutare;
void backtracking(int pos, int n){
if (pos == n+1){
for (int i=1; i<=n; i++){
cout<<permutare[i]<<" ";
}
cout<<'\n';
return;
}
for (int i=1; i<=n; i++){ //daca inversez for-ul -> fac ordine inversa lexicografica
if (folosit[i])
continue;
folosit[i] = true;
permutare[pos] = i;
backtracking(pos+1, n);
folosit[i] = false;
}
}
int main()
{
int n;
cin>>n;
folosit.resize(n+1, false);
permutare.resize(n+1);
backtracking(1, n);
/*
//CUM AM REZOLVA CU FOR-URI PT N=3
// -> PROBLEMA: NU STIU CATE FOR-URI SA PUN IN FUNCTIE DE N
// -> + COD URAT (ADICA PT N=8 AR TREBUI SA PUN 8 FOR-URI
int n = 3;
folosit.resize(n+1, false);
permutare.resize(n+1);
int pos = 1;
for (int i=1; i<=n; i++){
if (folosit[i])
continue;
folosit[i] = true;
permutare[pos] = i;
pos = pos + 1;
for (int i=1; i<=3; i++){
if (folosit[i])
continue;
folosit[i] = true;
permutare[pos] = i;
pos = pos + 1;
for (int i=1; i<=3; i++){
if (folosit[i])
continue;
folosit[i] = true;
permutare[pos] = i;
pos = pos + 1;
//afisare (pos == n+1)
for (int i=1; i<=n; i++){
cout<<permutare[i]<<" ";
}
cout<<'\n';
pos = pos - 1;
folosit[i] = false;
}
pos = pos - 1;
folosit[i] = false;
}
pos = pos - 1;
folosit[i] = false;
}
*/
return 0;
}