permutation recursion and backtracking
can someone explain me on a example like "ABC" how the program works ? as detailed as you can please. (im kinda stuck at recursion part)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <iostream>
#include <string>
using namespace std;
int total;
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char *sir, int i, int n)
{
int j;
if (i == n)
{
cout << sir << endl;
total += 1;
}
else
{
for (j = i; j <= n; j++)
{
swap((sir + i), (sir + j));
permute(sir, i + 1, n);
swap((sir + i), (sir + j));
}
}
}
int main()
{
char sir[50] ;
cout << "Introdu sirul decaractere: "; cin >> sir;
cout << "\n\n";
int n = strlen(sir) - 1;
permute(sir, 0, n);
cout << "\n\n";
cout << "Permutari: " << total;
system("pause>nul");
}
|
Have you tried setting a breakpoint on line 55 and then stepping through the program with the debugger?
well ty anyway i figured it myself I neededl ike 1-2 hours to fully understand the algorithm
Topic archived. No new replies allowed.