pleatofthepants wrote:
> Ok, I have to solve the "Rat Maze" problem"
>
> I am opening a file called maze.txt which looks similar to this
>
> 20 *this is # of rows
> 20 *this is # of columns
> 9 *starting row
> 9 *starting column
> 11111111111111101111
> 10101010101010101011
> 10000010001000101001
> 10111000100010000011
> 10111111111111111011
> etc....
>
> I have to find the path out of the maze using recursion
>
> This is what I have come up with for the recursion function, ant tips?
>
> int maze(int data[][])
> {
> data[r][c];
>
> if( data[r][0] | data[0][c] | data[r][19] | data[19][c] == 0) //
> base case
> {
> cout << "The Rat Is Free!" << endl;
> }
> if( data[r+1][c] == 0 )
> {
> cout << "Move Up" << endl;
> return maze(data[r+1][c]);
> }
>
> if ( data[r-1][c] == 0 )
> {
> cout << "Move Down" << endl;
> return maze(data[r-1][c]);
> }
>
> if ( data[r][c+1] == 0 )
> {
> cout << "Move Right" << endl;
> return maze(data[r][c+1]);
> }
>
> if ( data[r][c-1] == 0 )
> {
> cout << "Move Left" << endl;
> return maze(data[r][c-1]);
> }
>
> else
> {
> cout << "Rat your are stuck" << endl;
> }
> }
There is an algorithm for pathfinding that would work perfect for this, but
I'm fairly sure this is homework and the professor wants you to come up with
your own algorithm.
Think of it this way though as a general idea.
Look at your current position and see if it is free.
If not call this function with one position north.
If not call this function with one positon south.
If not call this function with one postion east.
If not call this function with one positon south.
That is a very base and doesn't take into account a lot of things which you
need to code for. The function will need to be called wtih the postion to
check, and also need to return something indicating what it found there.
Impassible, opne or free.
Also, it won't find the most effecient way out, just a way out.
Another possibiltiy is the right wall way. You follow a right wall (or left)
until you are out. This works in all mazes such as this unless the walls
are not continiguous.
I don't want to give too much because this is info, but the function you
have shown is not recursive at all. A recursive function calls itself.
--
Jim Langston