473,813 Members | 2,802 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursive Backtracking

Ok, I'm working on this program to do recursive backtracking, however
it's not working for all cases. Any help would be apprietiated. (all 4
should be true, but 168 is coming up false)

Heres the rules:
1. If n is even, then you may give back exactly n/2 bears.
2. If n is divisible by 3 or 4, then you may multiply the last two
digits of n and give back this many bears. By the way, the last digit
of n is n%10, and the next-to-last digit is ((n%100)/10. Keep in mind
that the result of these operations may be 0, in which case, you don't
want to use this option.
3. If n is divisible by 5, then you may give back exactly 42 bears.
The goal of the game is to end up with EXACTLY 42 bears.

Heres the code:
#include <iostream>
using namespace std;
bool bear (int);

void main( )
{
cout << bear(168) << endl;
cout << bear(250) << endl;
cout << bear(42) << endl;
cout << bear(84) << endl;
}

bool bear(int x)
{
if (x<42)
return false;
if(x==42)
return true;
if(x%3==0 || x%4==0)
{ /* rule 2, some cases eliminates rule 1 and rule 3 */
int a = (x%10 * ((x%100)/10));
if(a!=0)
return bear(x-a);
/* if the last two digits give 0 we don't want to use that
* So what should we do ??
* The scenario for this is:
* a) 0x
* - gets caught by rule 1, 50% of the time and rule 3,
10% of the time
* b) x0
* - gets caught by rule 3 100% of the time
*/
}
if(x%5==0) /* rule 3 and in some cases eliminates rule 1 */
return bear(x-42);
if(x%2==0) /* rule 1 */
return bear(x/2);
/* what should we do if nothing matches (like 59) ??
* Since we got no rule to catch that, we better return false
*/
return false;
}

Dec 15 '06 #1
2 3436
KS
bool bear(int x)
{
if (x<42)
return false;
if(x==42)
return true;
if(x%3==0 || x%4==0)
{ /* rule 2, some cases eliminates rule 1 and rule 3 */
int a = (x%10 * ((x%100)/10));
You probably want to apply rules in the order given by your
description. (You are applying rule 2 before rule 1)

Dec 16 '06 #2
Ok, I'm working on this program to do recursive backtracking, however
it's not working for all cases. Any help would be apprietiated. (all 4
should be true, but 168 is coming up false)

Heres the rules:
1. If n is even, then you may give back exactly n/2 bears.
2. If n is divisible by 3 or 4, then you may multiply the last two
digits of n and give back this many bears. By the way, the last digit
of n is n%10, and the next-to-last digit is ((n%100)/10. Keep in mind
that the result of these operations may be 0, in which case, you don't
want to use this option.
3. If n is divisible by 5, then you may give back exactly 42 bears.
The goal of the game is to end up with EXACTLY 42 bears.
try:

#include <iostream>

bool bear(int x);

int main()
{ std::cout << std::boolalpha ;
std::cout << bear(168) << std::endl;
std::cout << bear(250) << std::endl;
std::cout << bear(42) << std::endl;
std::cout << bear(84) << std::endl;
return 0;
}

bool bear(int x)
{ if (x<42)
return false;

if (x==42)
return true;

if (x%2==0)
if (bear( x/2 ))
return true;

if ((x%3==0)||(x%4 ==0))
if ( int a =((x%10)*((x%10 0)/10)) )
if (bear( x - a))
return true;

if (x%5==0)
if (bear( x-42 ))
return true;

return false;

}
Dec 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1948
by: Steven Spear | last post by:
Hi. I am trying to perform backtracking with this search function. Something is going wrong when I enter 2 at the command line. Entering 1 at the command line seems to work fine. I notice that backtracking never takes place. Obviously there is something wrong with my logic. I have been trying many things for many days to correct the situation, but nothing seems to help. Does anyone have any suggestions? Thanks, Steve #include...
20
17051
by: Webdad | last post by:
Hi! I running my first year as industrial engineer (informatics) We have an assignment to do : .... create a playfield (matrix). Some places in that field are blocked, so you can't pass them. The others are free to go over ... (I already found that part) -> http://users.pandora.be/hebbrecht/jochen/c++/test.cpp
6
4536
by: Talin | last post by:
I've been using generators to implement backtracking search for a while now. Unfortunately, my code is large and complex enough (doing unification on math expressions) that its hard to post a simple example. So I decided to look for a simpler problem that could be used to demonstrate the technique that I am talking about. I noticed that PEP 255 (Simple Generators) refers to an implementation of the "8 Queens" problem in the lib/test...
9
3330
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
5
2456
by: ChuckB | last post by:
Ok, heres the code i've come up with #include <iostream> using namespace std; bool bear (int); void main( ) { bear(250); // is true bear(42); // is true
12
5834
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an asterisk is in a blob, an asterisk that is contiguous to it is in the same blob. If a blob has more than two asterisks, then each asterisk in the blob is contiguous to at least one other asterisk in the blob. For example this 12x12 grid has 6 blobs. ...
18
4734
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: from pyparsing import * grammar = OneOrMore(Word(alphas)) + Literal('end') grammar.parseString('First Second Third end')
1
1500
by: ghael | last post by:
what is the importance of recursive backtracking and how does it works?
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9607
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10140
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9224
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7684
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5706
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.