我正在开发一个程序,该程序将读取一个包含迷宫信息的文件,并使用该信息找到穿过迷宫的路径。我遇到的问题是在数组中表示迷宫。我不知道如何将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
要表示迷宫的连通性,可以将迷宫中的一个位置与存在路径的相邻单元格列表相关联。该列表可以是一个实际的列表,也可以是一种更紧凑的表示,如位图:例如,1010可能意味着我们可以向北和向南,但不能向东和向西。
您可能还发现,使用比迷宫大的阵列(即,在阵列中包括迷宫周围的边界单元)很有帮助,这样就可以参考迷宫单元的相邻单元,而不必担心边缘条件。