Contents
Recursion
Recursion problem can be represented as follows:
void Recursion(T input)
{
// Base case
if(input == BaseCaseValue)
{
// Perform operation if needed
return;
}
else
{
// Recursive case:
// Perform operation
// Modify input to reach base case
Recursion(modifiedInput);
}
}
Backtracking
Backtracking problem can be represented as follows:
bool Recursion(T input)
{
// Base case
if(Condition_Satisfied(input))
{
// Perform operation if needed
return true;
}
else
{
// Select
// Explore all the remaining choices
for(auto nextChoice : nextChoices)
{
if(Recursion(nextChoice))
return true;
}
// Backtrack
}
}
N-Queens Problem
Click to expand C++ Code
Cindy’s Puzzle
Click to expand C++ Code
Solving a maze
Click to expand C++ Code