473,788 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Backtracking Search Function Trouble

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 <iostream>
#include <vector>
#include <stack>
#include <string>

using namespace std;

template <class T>
struct State
{
bool visited;
vector<State*> children;
T* data;

State () : visited(false), data(0) { }
virtual ~State () { }

virtual void print (int indent = 0) = 0;
virtual bool is_solution () = 0;
virtual void generate_childr en () = 0;
};

template <class T>
bool search (State<T>& initial) //trouble with this fcn
{
stack<State<T>* > s;
State<T>* tmp;
tmp=&initial;
bool runner=true;
while(runner)
{
if(tmp->is_solution( ))
{
tmp->print(s.size() *2);
cout<<"Solution found!\n";
return true;
}
else
{
tmp->print(s.size() *2);
tmp->visited=true ;
}

tmp->generate_child ren();

int i;
for(i=0; i<tmp->children.size( )
&& tmp->children[i]->visited; i++){}

if(!tmp->children[i]->visited)
{
cout<<"Going to child...\n";
s.push(tmp);
tmp=tmp->children[i];
continue;
}
else if(tmp->children[i]->visited && s.empty())
{
cout<<"No solution exists!\n";
return false;
}
else if(tmp->children[i]->visited && !s.empty())
{
tmp=s.top();
cout<<"Backtrac king...\n";
s.pop();
continue;
}
}

}

struct mt3data
{
char ary[3][3];
};

struct mt3 : public State<mt3data>
{
mt3 ();
~mt3 ();

void print (int indent = 0);
bool is_solution ();
void generate_childr en ();

void copy (const mt3& tocopy);
};

mt3::mt3 () {
data = new mt3data;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
data->ary[i][j] = '_';
}

mt3::~mt3 () { delete data; }

void mt3::print (int indent) {

string theindent = "";
for (int i = 0; i < indent; ++i)
theindent += " ";

for (int i = 0; i < 3; ++i) {
cout << theindent;
for (int j = 0; j < 3; ++j)
cout << data->ary[i][j];
cout << endl;
}
}

bool mt3::is_solutio n () {
for (int i = 0; i < 3; ++i) {
int rowcount = 0, colcount = 0;

for (int j = 0; j < 3; ++j) {
if (data->ary[i][j] == 'X') ++rowcount;
if (data->ary[j][i] == 'X') ++colcount;
}

if (rowcount == 3 || colcount == 3) return true;
}

int left = 0, right = 0;
for (int i = 0; i < 3; ++i) {
if (data->ary[i][i] == 'X') ++left;
if (data->ary[i][2-i] == 'X') ++right;
}

if (left == 3 || right == 3) return true;

return false;
}

void mt3::generate_c hildren() {

if (!children.empt y()) return;

for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
if (data->ary[i][j] == '_') {
mt3* tmp = new mt3;
tmp->copy(*this);
tmp->data->ary[i][j] = 'X';
children.push_b ack(tmp);
}
}

void mt3::copy (const mt3& tocopy) {
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
data->ary[i][j] = tocopy.data->ary[i][j];
}

void syntax (string s) {
cout << "Specify an integer argument as to which test to run: "
<< endl
<< "1. Successful MT3" << endl
<< "2. Unsuccessful MT3" << endl;
cout << endl;
cout << "For example: " << s << " 2" << endl;
}
int main (int argc, char** argv) {
if (argc == 1) {
syntax(argv[0]);
return 0;
}

string argument = argv[1];

if (argument == "1") {
mt3 goodmt3;

goodmt3.data->ary[0][2] = '0';
goodmt3.data->ary[1][2] = '0';
goodmt3.data->ary[2][2] = '0';

search(goodmt3) ;
return 0;
} else if (argument == "2") {
mt3 badmt3;

badmt3.data->ary[0][0] = '0';
badmt3.data->ary[1][1] = '0';
badmt3.data->ary[2][2] = '0';

search(badmt3);
return 0;
}

syntax(argv[0]);

return 0;
}
Jul 22 '05 #1
1 1946
Hi Martin. If you (or anyone else) want(s) me to answer this question, I
will if I have time soon. Right now I'm in the middle of something. Send me
an email, and I'll get back to you.

Meanwhile, I have discovered the answer to my question regarding this
problem.

Thanks, Steve

"Martin Magnusson" <lo*******@frus tratedhousewive s.zzn.com> wrote in message
news:35******** *************** ***@posting.goo gle.com...
sm*****@hotmail .com (Steven Spear) wrote in message
news:<66******* *************** ****@posting.go ogle.com>...
string argument = argv[1];

if (argument == "1") {
mt3 goodmt3;

goodmt3.data->ary[0][2] = '0';
goodmt3.data->ary[1][2] = '0';
goodmt3.data->ary[2][2] = '0';

search(goodmt3) ;
return 0;
} else if (argument == "2") {
mt3 badmt3;

badmt3.data->ary[0][0] = '0';
badmt3.data->ary[1][1] = '0';
badmt3.data->ary[2][2] = '0';

search(badmt3);
return 0;
}


What is the command line argument supposed to mean? And why do you
have different indexes for goodmt3 and badmt3 (for goodmt3 you
initialize [0][2], [1][2] and [2][2], while for badmt3 you do
[0][0],[1][1] and [2][2])?

/ martin

Jul 22 '05 #2

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

Similar topics

3
2101
by: shoo | last post by:
I am writing a program on Backtracking recursion in search of the gold. but I don't know how to start my search at location (1,1), which should be Island in a empty space character.. Here is what I have done so far: http://yuricoco.yu.ohost.de/111.cpp and I should mrak very point that I have searched...like this http://yuricoco.yu.ohost.de/finish.txt
6
4532
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...
2
1387
by: Gi | last post by:
Hello, I'm a new Php user. I'm trying to use the array functions and would like to ask a suggestion, if possible. I wrote the following sample code: <?php $test="tizio"; $test="caio";
12
5832
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. ...
3
2252
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I have a VB app, and I have been working at it for a while, and I am now at the stage where I want to create a search function. Now don't be scared! It is in the .Net compact framework, and uses SQL Server CE as the database (This seems to scare off people trying to help! lol) but the connection and reading of data etc is all handled, and I think it is going to be a "relatively" simple function. My database has a number of fields...
1
1757
by: ROMANIA | last post by:
Dear mr. enginer Please help me to rezolv this prolem. We have some arrays: A1 1 2 3 4 5 6 14 17 23 24 27 33 A2 7 11 13 17 19 20 25 A3 23 25 26 27 A4 34 45 46 47 48 49 A5 1 5 15 17 18 23 26 29 33 34 35 36 39 46 47
4
1387
by: =?Utf-8?B?V2lsbGlhbSBQb3dlbGw=?= | last post by:
I've having trouble searching within the microsoft.public.dotnet.framework.microframework newsgroup. In the Search For: box, I enter "porting" (no quotes), select the newsgroup as listed above, and click Go. The screen repaints with no postings found - but I have clearly seen the word "porting" in the subject of one of the messages. Is this feature "broken"?
1
1226
by: ashraf02 | last post by:
Someone please help! i am writing a code for a search function and everytime i execute the code i get the following error. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0,10' at line 1 i have tried to remove the line of code to see wat happens ia search result appears but with errors. can someone please help sort this problem. <?php $get =...
1
13040
by: DeZZar | last post by:
For anyone that has the 'pleasure' of using Office 2007 you will know of one handy feature built in - its the search function housed in the record selector bar at the bottom of a form that allows to basically start typing and it will automatically search any of the fields within the form for matches and navigating to the first matching record as you type.... I would really like to be able to build this function into an Access 2003...
0
9498
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
10366
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...
1
10112
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
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...
1
7518
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
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.