Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help on this C++ Program For this program you\'ll write a program that us

ID: 3690860 • Letter: P

Question

Please help on this C++ Program

For this program you'll write a program that uses a recursive function to carry out a depth-first search.

The Teddy Bear Game is played as follows:

You start the game with N teddy bears, 0 < N <= 10,000.

You may ask for or give back teddy bears according to these rules:

If the number of teddy bears you have is an even number, you can turn in (remove) exactly half the teddy bears you have.

If the number of teddy bears you have is a multiple of 3, you can ask for (receive) one third as many teddy bears as you currently have. (So if you have 33, you can ask for another 11.)

You can always ask for (receive) exactly 23 teddy bears.

There is no priority among the rules; you may choose any rule that applies in a given situation.

The number of teddy bears you hold must always be greater than 0 and can never be more than10,000.

The object of the game is to end with exactly 17 teddy bears in no more than 20 turns.

Your program will ask the user for the starting number of teddy bears, and then use a recursive function to search for a solution. Your program must use recursion to receive full credit. Output for each case is either a statement that no solution within 20 turns exists (determined after exhaustivesearch) or a listing of what choices are necessary to end with exactly 17. Because you'reusing a recursive function, it may be simpler to list it from the last step going backwards towards the beginning; that's fine.Allow the user to enter multiple values for N. If the user enters values out of range, prompt for reentry.Ask the user after each case whether to continue.

Explanation / Answer

#include <stdio.h>
int dfs(int n,int count)
{
//   printf("DFS==%d --- %d ",n,count);
   int r;
   if(count>20)
   {
       return 0;
   }
   else if(n==17){
       printf("Stack n=%d ",n);
       return 1;
   }
   else if(n%2==0)
   {
       r=dfs(n/2,count+1);
       if(r==1)
       {
           printf("Stack n=%d ",n);  
           return 1;
       }

   }
   if(n+23<=1000)
   {
       r=dfs(n+23,count+1);
       if(r==1)
       {
           printf("Stack n=%d ",n);  
           return 1;
       }
   }
  
   if(n%3==0 && n+n/3<=1000)
   {
       r=   dfs(n+n/3,count+1);
       if(r==1)
       {
           printf("stack n=%d ",n);  
           return 1;
       }
   }
   else return 0;
}

int main() {
   char ch='y';
   int n=1001;
   while(ch=='y' || ch=='Y'){
      
       while(n>1000 || n<0){
           printf("Please Enter the initial Number ");
           scanf("%d",&n);
       }
       printf("yes %d ",n);
       int r= dfs(n,0);
       if(r==0){
           printf("Not Possible ");
       }
       else{
           printf("Return is %d ",r);
       }
       printf("Do you want to Continue: ");
       scanf("%c ",&ch);
       printf("choice:%c ",ch);
   }
  
  
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote