Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to leave recursion prematurely?

Tags:

c++

recursion

My current recursive function works to a degree, but then ruins itself when it goes back down the stack.

void Graph::findPath( Room * curRoom )
{
if( curRoom -> myNumber == 0 )
{
    cout << "Outside.\n";
    //Escape the recursion!
}
else
{
    curRoom -> visited = true;

    if( curRoom -> North -> visited == false )
    {   

        escapePath[ _index ] = "North";
        cout << "_index: " << _index << "\n";
        ++_index;

        findPath( curRoom -> North );

        cout << "_index: " << _index << "\n";
        escapePath[ _index ] = "";
        --_index;
    }

    if( curRoom -> East -> visited == false )
    {   
        escapePath[ _index ] = "East";
        cout << "_index: " << _index << "\n";
        ++_index;

        findPath( curRoom -> East );

        cout << "_index: " << _index << "\n";
        escapePath[ _index ] = "";
        --_index;
    }

    if( curRoom -> South -> visited == false )
    {   
        escapePath[ _index ] = "South";
        cout << "_index: " << _index << "\n";
        ++_index;

        findPath( curRoom -> South );

        cout << "_index: " << _index << "\n";
        escapePath[ _index ] = "";
        --_index;
    }

    if( curRoom -> West -> visited == false )
    {   
        escapePath[ _index ] = "West";
        cout << "_index: " << _index << "\n";
        ++_index;

        findPath( curRoom -> West );

        cout << "_index: " << _index << "\n";
        escapePath[ _index ] = "";
        --_index;
    }
}
}

To save you some reading, the idea is that the base case is finding 0. Otherwise it tries four different cardinal directions, which is a different numbered room. Every time it makes a move, it adds the move it made to an external array, and every time it comes back up it removes that step from the stack.

My issue is that it has the correct path stored when it finds the 0, but removes it on the way back up.

Is there a way to escape it, such as a break.

No gotos or exceptions

like image 274
Joshua Avatar asked Dec 11 '11 08:12

Joshua


People also ask

How do I get out of recursion early?

A first way to escape recursion is to evaluate everything then return 0 when the input list is empty. A second way to escape recursion is to evaluate everything but the last element, then either return the last thing or do something to the last thing and then return the result of that last function.

Can you break out of a recursive function?

You don't "break" out of recursive functions. Trying to do so says you're thinking about them the wrong way. Currently your recursive call is ignoring the output, which means that the recursion is pointless; whatever is_pal(middle(str)) returns has no effect on the return value of your function.

When should you stop recursion?

A recursive function must have at least one condition where it will stop calling itself, or the function will call itself indefinitely until JavaScript throws an error. The condition that stops a recursive function from calling itself is known as the base case.

How do I get out of recursion in CPP?

exceptions (or the C longjmp ) are the way to leave a recursion. You might also have a static boolean leaveme; flag and start your function with if (leaveme) return; etc. But longjmp don't work nicely with destructors of values in local frames (while exceptions do).


1 Answers

There is a way to exit recursion using exceptions, but I wouldn't recommend it. Instead, modify your function to return a bool that indicates whether you've found 0 or not and modify your logic to return from the function without changing path if 0 has been found. Here's the illustration of the idea:

bool Graph::findPath( Room * curRoom )
{
    if( curRoom -> myNumber == 0 )
    {
        cout << "Outside.\n";
        //Escape the recursion!
        return true;
    }
    // ...
    if (findPath( curRoom -> North ))
        return true;
    // ...
    return false;
}
like image 144
vitaut Avatar answered Sep 22 '22 21:09

vitaut