C++ 利用栈实现走迷宫

来源:互联网 发布:白俄罗斯交友软件 编辑:程序博客网 时间:2024/06/05 00:23

使用两个栈,一个用于初始化迷宫,一个用于回溯。

#include <iostream>#include <string>#include <cstring>#include <stack>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{private:    int x, y;    friend class Maze;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;    }};class Maze{private:    Cell currentCell;   //当前位置    Cell exitCell;      //出口位置    Cell entryCell;     //初始位置    const char exitMarker, entryMarker, visited, passage, wall; //描述迷宫的变量    Stack<Cell> mazeStack;  //迷宫    char **store;    void pushUnvisited(int row, int col);    friend ostream &operator<<(ostream &out, const Maze &maze);    int rows, cols;public:    Maze();    void exitMaze();};Maze::Maze():exitMarker('e'), entryMarker('m'), visited('.'), passage('0'), wall('1'){    Stack<char *> mazeRows;    char str[80], *s;    int col, row = 0;    cout << "enter a regular maze using  the following "         << "characters:\nm - entry\ne - entry\ne - exit\n1 - wall\n0 - passage\n"         << "enter one line at a time; end with ctrl-z:\n";    while(cin >> str){        row++;        cols = strlen(str);        s = new char[cols + 3];        mazeRows.push(s);        strcpy(s+1, str);        s[0] = s[cols + 1] = wall;        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];    store[0] = new char[cols+3];    for( ; !mazeRows.empty(); row--)        store[row] = mazeRows.pop();    store[rows+1] = new char[cols+3];    store[0][cols+2] = store[rows+1][cols+2] = '\0';    for(col = 0; col <= cols+1; col++){        store[0][col] = wall;        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;        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";}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;}int main(){    Maze().exitMaze();    return 0;}

有空补上解析