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;
}
}