473,387 Members | 1,891 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,387 software developers and data experts.

Backtracking recursion problem

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[1][1] 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
but it didn't show it on my code..
is any thing wrong?
thank!~
Jul 23 '05 #1
3 2081
shoo wrote:

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[1][1] 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
but it didn't show it on my code..
is any thing wrong?
thank!~


When I click on your links, then some server tells me
that 'external linking is not allowed on this server'

Why don't you just include the source code in your post?
Usually backtracking programs are not that long.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<42***************@gascad.at>...
shoo wrote:

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[1][1] 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
but it didn't show it on my code..
is any thing wrong?
thank!~


When I click on your links, then some server tells me
that 'external linking is not allowed on this server'

Why don't you just include the source code in your post?
Usually backtracking programs are not that long.


ok..I post it here....

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
char Island[13][13] ={ "~~~~~~~~~~~~",
"~ ~~~ ~~",
"~#### ~ ~",
"~# ## *** ~",
"~# # * ~",
"~# / / # ~",
"~ // VV# ~",
"~ # ~",
"~ !!! V **~",
"~ @! *** ~",
"~ !! ~* ~~~",
"~~~~~~~~~~~~" };

bool pathFound(int IndX,int IndY);
void printArray(char Island[13][13], int, int);

int main(int argc, char* argv[])
{
if(pathFound(9,3 ) )
cout << " found" << endl;
else
cout << "not found" << endl;
printArray(Island,13,13);

return 0;
}
bool pathFound(int IndX,int IndY)
{

if (Island[IndX][IndY] == '@') // if it is the Golden Floppy
{
//Stopping case, we have found! Mark the tail
Island[IndX][IndY] = '.';
cout<<"We have found the Golden Floppy"<<IndX<<" "<<IndY<<"\n";
return true;
}
//if that position contains are obstacles
if (Island[IndX][IndY] == '~') //if it is contains water
return false;

if (Island[IndX][IndY] == '#') //if it is contains thicket
return false;

if (Island[IndX][IndY] == 'V') //if it is contains gorge
return false;

if (Island[IndX][IndY] == '/') //if it is contains mountain
return false;

if (Island[IndX][IndY] == '!') //if it is contains big cats
return false;

if (Island[IndX][IndY] == '*') //if it is contains quicksand
return false;

if (Island[IndX][IndY] == ' ')
{
//if it is empty space/blank, try up, down, right, left
Island[IndX][IndY] = '.';

if(pathFound( IndX, IndY + 1 ) ||
pathFound( IndX, IndY - 1 ) ||
pathFound( IndX - 1, IndY ) ||
pathFound( IndX + 1, IndY ) )
{

cout <<"Path"<<IndX<<" "<<IndY<<endl;
return true;
}

}

return false;
}

void printArray(char Island[13][13], int m, int n)
{
for (int row =0; row<m; row++)
{
for (int col=0; col<n; col++)
{
cout<<setw(3)<<Island[row][col];
}
cout<<endl;
}
}
Jul 23 '05 #3

"shoo" <yu******@gmail.com> wrote in message
news:1a**************************@posting.google.c om...
Karl Heinz Buchegger <kb******@gascad.at> wrote in message
news:<42***************@gascad.at>...
shoo wrote:
>
> 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[1][1] in a empty space character..
>

You don't say what happens, or what goes wrong, but read on, and you might
see the fix...

ok..I post it here....

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
char Island[13][13] ={ "~~~~~~~~~~~~",
If I'm not mistaken, defining an array this way makes your first coordinate
the row (Y) and the second one the column (X), right? So later, when
checking the array location, you need to reverse the order of the check from
Island[IndX][IndY] to [IndY][IndX].
"~ ~~~ ~~",
"~#### ~ ~",
"~# ## *** ~",
"~# # * ~",
"~# / / # ~",
"~ // VV# ~",
"~ # ~",
"~ !!! V **~",
"~ @! *** ~",
"~ !! ~* ~~~",
"~~~~~~~~~~~~" };

bool pathFound(int IndX,int IndY);
void printArray(char Island[13][13], int, int);

int main(int argc, char* argv[])
{
if(pathFound(9,3 ) )
cout << " found" << endl;
else
cout << "not found" << endl;
printArray(Island,13,13);

return 0;
}
bool pathFound(int IndX,int IndY)
{

if (Island[IndX][IndY] == '@') // if it is the Golden Floppy
See my comment earlier about reversing those indices.
{
//Stopping case, we have found! Mark the tail
Island[IndX][IndY] = '.';
cout<<"We have found the Golden Floppy"<<IndX<<" "<<IndY<<"\n";
return true;
}
//if that position contains are obstacles
if (Island[IndX][IndY] == '~') //if it is contains water
return false;

if (Island[IndX][IndY] == '#') //if it is contains thicket
return false;

if (Island[IndX][IndY] == 'V') //if it is contains gorge
return false;

if (Island[IndX][IndY] == '/') //if it is contains mountain
return false;

if (Island[IndX][IndY] == '!') //if it is contains big cats
return false;

if (Island[IndX][IndY] == '*') //if it is contains quicksand
return false;

if (Island[IndX][IndY] == ' ')
{
//if it is empty space/blank, try up, down, right, left
Island[IndX][IndY] = '.';

if(pathFound( IndX, IndY + 1 ) ||
pathFound( IndX, IndY - 1 ) ||
pathFound( IndX - 1, IndY ) ||
pathFound( IndX + 1, IndY ) )
{
Either here, or at the start of the function, you need to make sure that the
index doesn't go negative or past the end of your array(s). Since you're
accepting int as parameters, which can be negative, it would probably be
easiest to simply add, at the _start_ of the function:

if ((IndX < 0) || (IndX > 12) || (IndY < 0) || (IndY > 12))
return false; // I've fallen off the end of the world!

cout <<"Path"<<IndX<<" "<<IndY<<endl;
return true;
}

}

return false;
}

void printArray(char Island[13][13], int m, int n)
{
for (int row =0; row<m; row++)
{
for (int col=0; col<n; col++)
{
cout<<setw(3)<<Island[row][col];
(Notice you have row then column? The row is your Y index, and col is your
X index. See earlier comments for why this matters.)
}
cout<<endl;
}
}

Jul 23 '05 #4

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

Similar topics

1
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...
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...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
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...
13
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
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...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.