Backtracking recursion problem

K

Karl Heinz Buchegger

shoo said:
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.
 
S

shoo

Karl Heinz Buchegger said:
shoo said:
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;
}
}
 
H

Howard

shoo said:
Karl Heinz Buchegger said:
shoo said:
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:

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top