1

I am working on a program that will read a file containing information about a maze and use that information to find a path through the maze. The problem I am having is representing the maze in the array. I am not sure how to turn the X's into integers to represent them as walls in the array. In the one argument constructor I use a for loop to go through the file and put each character into the appropriate array index. The way I have done this clearly does not work and if anybody could give some advice on how properly represent the maze in the array I would appreciate it.

   /**@file Maze.h*/

    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;

    const int MAX_ROW = 60;
    const int MAX_COL = 40;
    const int WALL = 0;
    const int CLEAR = 1;
    const int PATH = 2;
    const int VISITED = 3;

    struct Position
    {
        int r;
        int c;
    };
    class Maze
    {
    public:
    Maze();

    /*One argument constructor that takes a filename and reads
     *the contents of a maze file.
     */
    Maze(string filename);
    void displayMaze();
    bool isWall(Position p);
    bool isPath(Position p);
    bool isClear(Position p);
    bool isVisited(Position p);
    void setWall(Position p);
    void setPath(Position p);
    void setClear(Position p);
    void setVisited(Position p);
    Position getEntrance();
    Position getExit();
    private:
        int row;
        int col;
        Position exit;
        Position entrance;
        int maze[MAX_ROW][MAX_COL];
    };

/**@file Maze.cpp*/

#include "Maze.h"

Maze::Maze()
{}

Maze::Maze(string filename)
{
    ifstream inStream;
    inStream.open(filename.c_str());
    if(inStream.fail())
{
    cout << "Input file opening failed.\n";
}
//Get the dimensions of the maze.
inStream >> col >> row; 
//Get the exit to the maze.
inStream >> exit.r >> exit.c;
//Get the entrance to the maze.
inStream >> entrance.r >> entrance.c;
//Read maze from the file.
for(int r = 0; r < row; r++)
{   
    for(int c = 0; c < col; c++)
    {
        inStream >> maze[r][c];
        if(maze[r][c])== 'X')
            maze[r][c] = WALL;
        else
            maze[r][c] = CLEAR;
    }
}
}//end one argument constructor

void Maze::displayMaze()
{
cout << '\t' << '\t' << "Row" << '\t' << "Column" << endl;
cout << "Dimensions:" << '\t' << row << '\t' << col << endl;
cout << "Exit:" << '\t' << '\t' << exit.r << '\t' << exit.c << endl;
cout << "Entrance: " << '\t' << entrance.r << '\t' << entrance.c << endl;

for(int r = 0; r < row; r++)
{
    for(int c = 0; c < col; c++)
    {
        cout << maze[r][c];
    }
    cout << endl;
}
}//end displayMaze()

bool Maze::isWall(Position p)
{
return maze[p.r][p.c] == WALL;
}//end isWall()

bool Maze::isPath(Position p)
{
return maze[p.r][p.c] == PATH;
}//end isPath()

bool Maze::isClear(Position p)
{
return maze[p.r][p.c] == CLEAR;
}//end isClear()

bool Maze::isVisited(Position p)
{
return maze[p.r][p.c] == VISITED;
}//end isVisited()

void Maze::setWall(Position p)
{
maze[p.r][p.c] = WALL;
}//end setWall()

void Maze::setPath(Position p)
{
maze[p.r][p.c] = PATH;
}//end setPath()

void Maze::setClear(Position p)
{
maze[p.r][p.c] = CLEAR;
}//end setClear()

void Maze::setVisited(Position p)
{
maze[p.r][p.c] = VISITED;
}//end setVisited()

Position Maze::getEntrance()
{
return entrance;
}//end getEntrance()

Position Maze::getExit()
{
return exit;
}//end getExit()

Here is an example of what the file contents will look like. The first set of numbers is the dimensions of the maze in columns and rows. The second set of numbers is row and column position of the exit and the third set of numbers is the row and column position of the entrance.

20 7
0 18
6 12
xxxxxxxxxxxxxxxxxx x
x     x       xxxx x
x xxxxx xxxxx   xx x
x xxxxx xxxxxxx xx x
x x          xx xx x
x xxxxxxxxxx xx    x
xxxxxxxxxxxx xxxxxxx
4

1 回答 1

2

为了表示迷宫的连通性,您可以将迷宫中的位置与存在路径的相邻单元的列表相关联。该列表可以是一个实际的列表,或者更紧凑的表示形式,例如位图:例如 1010 可能意味着我们可以向北和向南,但不能向东和向西。

您可能还会发现使用比迷宫大的数组(即包括数组中迷宫周围的边界单元)很有帮助,这样可以引用迷宫单元的相邻单元而不必担心边缘条件。

于 2012-03-19T17:33:35.600 回答