私はC++を学んでいるので、学校の課題に取り組んでいます。私は、この問題の正しいアルゴリズムを理解/考え出すのに役立つように、提供されるコードをあまり探していません。
1 と 0 の (5x5x5) 3D 迷路を作成する必要があります。ランダムに入力します (ただし、0,0,0 は開始の 1 であり、4,4,4 は終了の 1 です。
これが私がやったことです。立方体オブジェクトを作成しました:
#include "cube.h"
Cube :: Cube(int cube_value)
{
cube_value = 0;
chk_up = false;
chk_down = false;
chk_left = false;
chk_right = false;
chk_front = false;
chk_back = false;
}
Cube :: ~Cube(void){}
私の迷路管理クラスでは、このように初期化します
PathFinder::PathFinder()
{
// initializing / sizing 5x5x5 Maze
Maze.resize(5);
for(int y = 0; y < 5 ; ++y)
{
Maze[y].resize(5);
for(int z = 0; z<5 ; ++z)
{
Maze[y][z].resize(5);
}
}
int at_x = 0;
int at_y = 0;
int at_z = 0;
}
そのクラスのヘッダー: #include "PathfinderInterface.h" #include "cube.h"
class PathFinder : public PathfinderInterface {
private:
int at_x;
int at_y;
int at_z;
public:
vector<vector<vector<Cube> > > Maze;
PathFinder();
virtual ~PathFinder();
string getMaze();
void createRandomMaze();
bool importMaze(string file_name);
vector<string> solveMaze();
};
だから私はそれを入力しようとしていますが、これは私が持っているものです。あまり意味がないかもしれません:
void PathFinder :: fillmaze()
{
Maze[0][0][0].cube_value = 1;
Maze[4][4][4].cube_value = 1;
int atx = 0 , aty = 0 , atz = 0;
while(atx<5 && aty < 5 && atz < 5)
{
if(atz == 5)
{
aty = aty + 1;
}
if(aty == 5)
{
atx = atx + 1;
atx = 0;
}
for(atz=0 ; atz<5 ; ++atz)
{
if((atx!= 0 && aty!=0 && atz!=0) || (atx!=4 && aty!=4 && atz!= 4) )
{
Maze[atx][aty][atz].cube_value = (rand() % 2);
}
}
}
}
すべての z 軸を塗りつぶし、x を右にトレースしてから、y を 1 つ上に移動して同じことをしようとしていますが、これは良いアプローチですか、それともこれを行うためのより良い方法はありますか? 私はちょうどかなり混乱しています。