473,800 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2101
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******@gasca d.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(Isla nd,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<<en dl;
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.goo gle.com...
Karl Heinz Buchegger <kb******@gasca d.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(Isla nd,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<<en dl;
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
1947
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...
6
4534
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...
43
4173
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
5646
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 complexity as I go. Here's a simple one I tried out: #include<stdio.h> /* To compare the the time and space cost of iteration against
18
3727
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 python is generally more time-efficient than recursion. Is that true? Here is my algorithm as it stands. Any suggestions appreciated!
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. ...
13
2916
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
1758
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
35
4742
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
9690
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
9551
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
10504
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
10274
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
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
5469
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...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
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.