6 Replies - 634 Views - Last Post: 31 July 2012 - 06:50 PM Rate Topic: -----

#1 KraKen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-April 12

maze program must rewrite using recursion

Posted 31 July 2012 - 03:13 PM

ok i am a programming student and my teacher asked us to build a program that reads in a file that has a specific maze in it
ex> mazetest1.txt
111111
111001
1000e1
100m11
111111

and trys to get out of the maze. ok my code works and i brought it to my professor for a final check. he says i now must shorten the code by getting rid of the stacks and use recursion only. i just worked hard to create this program and now he is basically changing the assignment on me near the deadline (tomorrow, and ive been giving him routine looks at my code so this last minute crud is B.S. personally) . ok so i have NO clue what to do to convert this to using only recursion and no stacks(or stl stack library) if anyone can help that would be extremely grateful

so the problem is basically converting the current code to one using only recursion and no stack use at all(code bellow)



a_mazing.cpp
#include <iostream>
#include <string>
#include <stack>
#include <cstring>

using namespace std;

template<class T>
class Stack : public stack<T> {
public:
    T pop() {
        T tmp = stack<T>::top();
        stack<T>::pop();
        return tmp;
    }
};

class Cell {
public:
    Cell(int i = 0, int j = 0) {
        x = i; y = j;
    }
    bool operator== (const Cell& c) const {
        return x == c.x && y == c.y;
    }
private:
    int x, y;
    friend class Maze;
};

class Maze {
public:
    Maze();
    void exitMaze();
private:
    Cell currentCell, exitCell, entryCell;
    const char exitMarker, entryMarker, visited, passage, wall;
    Stack<Cell> mazeStack;//stack
    char **store;         // array of strings;
    void pushUnvisited(int,int);
    int rows, cols;
    friend ostream& operator<< (ostream& out, const Maze& maze) {
        for (int row = 0; row <= maze.rows+1; row++)
            out << maze.store[row] << endl;
        out << endl;
        return out;
    }
};

Maze::Maze() : exitMarker('e'), entryMarker('m'), visited('.'),
               passage('0'), wall('1') {
   Stack<char*> mazeRows;//stack
    char str[80], *s;
    int col, row = 0;
    cout << "Enter a rectangular maze using the following "
         << "characters:\nm - entry\ne - exit\n1 - wall\n0 - passage\n"
         << "Enter one line at at time; end with Ctrl-z:\n";
    while (cin >> str) {
        row++;
        cols = strlen(str);
        s = new char[cols+3];    // two more cells for borderline columns;
        mazeRows.push(s);
        strcpy(s+1,str);
        s[0] = s[cols+1] = wall; // fill the borderline cells with 1s;
        s[cols+2] = '\0';
        if (strchr(s,exitMarker) != 0) {
             exitCell.x = row;
             exitCell.y = strchr(s,exitMarker) - s;
        }
        if (strchr(s,entryMarker) != 0) {
             entryCell.x = row;
             entryCell.y = strchr(s,entryMarker) - s;
        }
    }
    rows = row;
    store = new char*[rows+2];        // create a 1D array of pointers;
    store[0] = new char[cols+3];      // a borderline row;
    for ( ; !mazeRows.empty(); row--) {
        store[row] = mazeRows.pop();
    }
    store[rows+1] = new char[cols+3]; // another borderline row;
    store[0][cols+2] = store[rows+1][cols+2] = '\0';
    for (col = 0; col <= cols+1; col++) {
        store[0][col] = wall;         // fill the borderline rows with 1s;
        store[rows+1][col] = wall;
    }
}

void Maze::pushUnvisited(int row, int col) {
    if (store[row][col] == passage || store[row][col] == exitMarker) {
        mazeStack.push(Cell(row,col));
    }
}
void Maze::exitMaze() {
    int row, col;
    currentCell = entryCell;
    while (!(currentCell == exitCell)) {
        row = currentCell.x;
        col = currentCell.y;
        cout << *this;         // print a snapshot;
        if (!(currentCell == entryCell))
            store[row][col] = visited;
        pushUnvisited(row-1,col);
        pushUnvisited(row+1,col);
        pushUnvisited(row,col-1);
        pushUnvisited(row,col+1);
        if (mazeStack.empty()) {
             cout << *this;
             cout << "Failure\n";
             return;
        }
        else currentCell = mazeStack.pop();
    }
    cout << *this;
    cout << "Success\n";
}

int main() {
    Maze().exitMaze();
    return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: maze program must rewrite using recursion

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 3057
  • View blog
  • Posts: 9,304
  • Joined: 25-December 09

Re: maze program must rewrite using recursion

Posted 31 July 2012 - 03:31 PM

So what exactly don't you understand?

What have you tried?

You need to show some effort at completing this assignment, using the required recursive elements.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5673
  • View blog
  • Posts: 22,531
  • Joined: 23-August 08

Re: maze program must rewrite using recursion

Posted 31 July 2012 - 04:13 PM

Read this post might give you some ideas.

Then again, what you've provided seems to be a straight copy/paste from here (which in turn appears for come from this book), so not sure I hold out a lot of hope.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1933
  • View blog
  • Posts: 5,759
  • Joined: 05-May 12

Re: maze program must rewrite using recursion

Posted 31 July 2012 - 04:27 PM

And he's got other issues as well... The assignment was to read a file, but the code posted take input from cin.
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: maze program must rewrite using recursion

Posted 31 July 2012 - 06:28 PM

View PostSkydiver, on 01 August 2012 - 02:27 AM, said:

And he's got other issues as well... The assignment was to read a file, but the code posted take input from cin.


It looked like he was redirecting from the command line.


Inside exitMaze() you could call another function eg exitMazeRecursive(), which is recursive. It would basically replace the loop, and take row and column parameters at least and probably return a boolean.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1933
  • View blog
  • Posts: 5,759
  • Joined: 05-May 12

Re: maze program must rewrite using recursion

Posted 31 July 2012 - 06:43 PM

View Post#define, on 31 July 2012 - 06:28 PM, said:

View PostSkydiver, on 01 August 2012 - 02:27 AM, said:

And he's got other issues as well... The assignment was to read a file, but the code posted take input from cin.


It looked like he was redirecting from the command line.

Yeah, I looked at that on the OP before posting. Shouldn't a redirect into the program have been < rather than > ?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1933
  • View blog
  • Posts: 5,759
  • Joined: 05-May 12

Re: maze program must rewrite using recursion

Posted 31 July 2012 - 06:50 PM

View Post#define, on 31 July 2012 - 06:28 PM, said:

Inside exitMaze() you could call another function eg exitMazeRecursive(), which is recursive. It would basically replace the loop, and take row and column parameters at least and probably return a boolean.


+1. If he understood what the stack data structure was for, then this conversion should be really easy because now he is just using the system's stack instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1