【问题标题】:Reading a text file and creating a maze读取文本文件并创建迷宫
【发布时间】:2012-03-19 17:26:33
【问题描述】:

我正在开发一个程序,该程序将读取包含有关迷宫信息的文件,并使用该信息找到穿过迷宫的路径。我遇到的问题是在数组中表示迷宫。我不确定如何将 X 转换为整数以将它们表示为数组中的墙。在一个参数构造函数中,我使用 for 循环遍历文件并将每个字符放入适当的数组索引中。我这样做的方式显然行不通,如果有人可以就如何正确表示数组中的迷宫提供一些建议,我将不胜感激。

   /**@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()

以下是文件内容的示例。第一组数字是迷宫的列和行的尺寸。第二组数字是出口的行列位置,第三组数字是入口的行列位置。

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

【问题讨论】:

  • 它不起作用...以什么方式?
  • 在 for 循环中,我试图将迷宫的每一块读入数组。但是,将跳过 if 语句,仅执行 else 语句,因此所有空格都被标记为 CLEAR。我想知道如何读取文件,以便 X 在数组中表示为 0(零)。
  • 尝试更改您的数组以保存chars。

标签: c++ file-io maze


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 2021-11-19
    相关资源
    最近更新 更多