473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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_children () = 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_children();

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<<"Backtracking...\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_children ();

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_solution () {
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_children() {

if (!children.empty()) 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_back(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 1918
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*******@frustratedhousewives.zzn.com> wrote in message
news:35**************************@posting.google.c om...
sm*****@hotmail.com (Steven Spear) wrote in message
news:<66**************************@posting.google. 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
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...
6
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...
2
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
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...
3
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,...
1
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...
4
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,...
1
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.