Even knowing the equation that defines this recursive relationship would be VERY helpful thanks
The equation goes something like this:
- If the length of the string is 1, return the arrangement (base case).
- Otherwise, give each character a turn at being in the first position
and recursively call the rest of the string with that character removed.
This question is the same thing, by checking if there is 1 person standing, there is only one choice left,
otherwise produce all the arrangements that the next seat can be sat on by.
Try stepping through this simplified code here if it still doesn't make sense.
Otherwise, have a look at this solution:
#include <iostream>
#include <vector>
using namespace std;
vector<string> arrangements;
class Table
{
public:
string seated = "";
string notSeated = "";
Table(string n);
vector<string> dinner_shuffles(int n);
};
Table::Table(string n)
{
seated = "";
notSeated = n;
}
vector<string> Table::dinner_shuffles(int n)
{
//The names used should be the first n names as designated in the constructor.
notSeated = notSeated.substr(0, n);
if (notSeated.length() == 1)
{
arrangements.push_back(seated+notSeated);
}
for (int i = 0; i < notSeated.length(); i++)
{
Table newArrangement(notSeated.substr(0,i) + notSeated.substr(i+1, notSeated.length()));
newArrangement.seated = seated + notSeated[i];
newArrangement.dinner_shuffles(n);
}
//return all possible arrangements of n dinner guests as a vector of strings.
return arrangements;
}
int main()
{
//dinner guest names are fed in as a string
string notSeated = "abcde";
Table table(notSeated);
vector<string> vals = table.dinner_shuffles(3);
//abc, acb, bac, bca, cab, cba
for (int i = 0; i < vals.size(); i++){
cout << vals[i] << endl;
}
return 1;
}