A Teddy Bear Picnic Game
A Teddy Bear Picnic Game
Name
Institution
Course
Instructor
Date
Program Code
#include <iostream>
#include <string>
using namespace std;
// Recursive function to determine if it is possible to reach the goal of 42 bears
bool reachGoal(int numBears) {
int half = 0, lastTwoDigits = 0, minusFortyTwo = 0;
// Base case: goal reached
if (numBears == 42) {
return true;
}
// Base case: below goal
if (numBears < 42) {
return false;
}
// Case 1: if even, divide by 2
if (numBears % 2 == 0) {
half = reachGoal(numBears / 2);
}
// Case 2: if divisible by 3 or 4, subtract product of last two digits
if (numBears % 3 == 0 || numBears % 4 == 0) {
int lastDigit = numBears % 10;
int secondLastDigit = (numBears % 100) / 10;
int product = lastDigit * secondLastDigit;
if (product > 0) {
lastTwoDigits = reachGoal(numBears - product);
}
}
// Case 3: if divisible by 5, subtract 42
if (numBears % 5 == 0) {
minusFortyTwo = reachGoal(numBears - 42);
}
// If any path works, return true
return (half || lastTwoDigits || minusFortyTwo);
}
int main() {
int numBears;
cout << "Enter the number of bears you have: ";
cin >> numBears;
if (reachGoal(numBears)) {
cout << "Your goal is reached!" << endl;
} else {
cout << "Your goal cannot be reached!" << endl;
}
return 0;
}
Program Explanation
This program implements a recursive solution to the Teddy Bear Picnic problem, where the objective is to determine whether a given number of bears can be reduced to exactly 42 by following specific rules. The function reachGoal takes an integer input representing the number of bears and returns a Boolean value indicating whether the goal can be achieved.
Base Cases
The recursion is controlled by two base cases. First, if the number of bears equals 42, the function immediately returns true, indicating success. Second, if the number of bears drops below 42, the function returns false because it is no longer possible to reach the goal.
Recursive Cases
The function explores three possible operations based on the value of the input:
- If the number of bears is even, it recursively calls itself with half the number of bears.
- If the number is divisible by 3 or 4, it calculates the product of the last two digits and subtracts this value before making a recursive call.
- If the number is divisible by 5, it subtracts 42 and recursively evaluates the result.
Each of these operations represents a possible move in the game. The function explores all valid moves and checks whether any sequence of operations leads to the target value of 42.
Decision Logic
The results of the recursive calls are stored in variables and combined using a logical OR operation. If any of the recursive paths returns true, the function concludes that it is possible to reach the goal. Otherwise, it returns false.
Main Function
The main function serves as the entry point of the program. It prompts the user to input the number of bears and then calls the reachGoal function. Based on the returned result, it outputs whether the goal of reaching 42 bears is achievable.
Conclusion
This program demonstrates the use of recursion to solve a decision problem involving multiple conditions and branching paths. By systematically exploring all valid operations, the algorithm determines whether a solution exists. It highlights key programming concepts such as base cases, recursive calls, and logical decision-making, making it an effective example of problem-solving using recursion in C++.