Tips anyone

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pleatofthepants

    Tips anyone

    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
    111111111111111 01111
    101010101010101 01011
    100000100010001 01001
    101110001000100 00011
    101111111111111 11011
    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;
    }
    }
  • Klarth

    #2
    Re: Tips anyone

    On Feb 29, 2:17 pm, pleatofthepants <aaronWbry...@g mail.comwrote:
    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
    111111111111111 01111
    101010101010101 01011
    100000100010001 01001
    101110001000100 00011
    101111111111111 11011
    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;
    }
    >
    }
    The way it is currently written, it looks like you get some confusing
    output! For example, I can see that it is possible for your program's
    output to be:

    The Rat Is Free!
    Rat your are stuck

    Or even a combination of "The Rat Is Free" and one of the other
    movement messages (and make further unnecessary moves). Also, if your
    rat finds a dead end, how does your algorithm prevent going backwards
    and towards the start? I don't see how you keep track of which
    direction the rat came from, so it looks like your algorithm will
    retry going in the direction that you came from.

    On a large maze, it also looks like there is going to a heck of a lot
    of recursive calls. Perhaps you can move forward using a while loop
    and use recursive call to change or try different directions?

    Comment

    • Triple-DES

      #3
      Re: Tips anyone

      On 29 Feb, 11:19, "Jim Langston" <tazmas...@rock etmail.comwrote :
      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.
      His function is recursive, look at the points of exit.

      DP

      Comment

      • red floyd

        #4
        Re: Tips anyone

        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
        111111111111111 01111
        101010101010101 01011
        100000100010001 01001
        101110001000100 00011
        101111111111111 11011
        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[][])
        This line should not compile.
        {
        data[r][c];
        this line is bad.
        >
        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;
        }
        }

        Comment

        • Jim Langston

          #5
          Re: Tips anyone

          Triple-DES wrote:
          On 29 Feb, 11:19, "Jim Langston" <tazmas...@rock etmail.comwrote :
          >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.
          >
          His function is recursive, look at the points of exit.
          yes, you are right. I missed that. It's just not effective.

          --
          Jim Langston
          tazmaster@rocke tmail.com


          Comment

          Working...