2013-11-04 6 views
1

그래서 저는 C++를 배우면서 학교 과제를 수행하고 있습니다. 나는이 문제에 대한 올바른 알고리즘을 이해하는데 도움이되기 위해 나에게 주어질 코드를별로 찾고 있지 않다.임의의 "미로"생성 - 3D 배열 C++

1 및 0의 (5x5x5) 3d 미로를 생성해야합니다. ..

#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){} 
: 0, 0, 0을 제외하고 무작위로 (그것을 채우기 것은 시작에 대한 1 -4,4,4-되는 마무리에서 1 인

다음

내가 무슨 짓을했는지의 나는 큐브 객체를 만든 사용법 #include "PathfinderInterface.h" 사용법 #include "cube.h"

,691 : 내 미로 관리 클래스의

는 그 클래스에 대한이

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; 
} 

헤더처럼 초기화 나는 모든 z 축을 채우고로 추적을 시도하고있어

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); 
      } 
     } 
    } 
} 

:

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(); 
}; 

그래서 나는 그것을 채우기 위해 노력하고있어이 내가 가지고있는, 그것은 의미의 톤을하지 않을 수 있습니다 x에 오른 다음 y를 하나 올리고 똑같은 방법을 사용합니다. 좋은 접근 방법입니까 아니면 더 좋은 방법입니까? 나는 꽤 혼란스러워지고있다.

+0

이유가 있기 때문에 한 지점으로 이동해야하며 다른 곳에서 나와야합니까? – EvilTeach

+2

http://en.wikipedia.org/wiki/Maze_generation_algorithm을 읽으셨습니까? – n0rd

+0

EvilT - 미로가 한쪽 끝에서 시작하여 다른 쪽 끝에서 시작하므로 해당 지점의 경로에 유효한 번호가 있어야합니다. n0rd - 나는 그것을 보지 못했지만, 참고로 감사하겠다. –

답변

1
void PathFinder :: fillmaze() 
{ 
    int atx = 0 , aty = 0 , atz = 0; 
    while(atz<=4) 
    { 
    if(atx == 5) 
    { 
     aty = aty + 1; 
    } 
    if(aty == 5) 
    { 
     aty = 0; 
     atz = atz + 1; 
    } 
    if(atz < 5) 
    { 

      for(atx=0 ; atx<5 ; ++atx) 
      { 
        Maze[atx][aty][atz].cube_value = (rand() % 2); 
      } 

    } 
    } 
     Maze[0][0][0].cube_value = 1; 
     Maze[4][4][4].cube_value = 1; 

} 

이제 미로 통과! :/