Advertisement
Josif_tepe

Untitled

Oct 9th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4.  
  5. class Maze {
  6. private:
  7.     int rows, columns;
  8.     std::vector<std::vector<char> > mat;
  9.    
  10. public:
  11.     Maze(int _rows = 2, int _columns = 2) {
  12.         rows = _rows;
  13.         columns = _columns;
  14.         mat.resize(rows);
  15.         for(int i = 0; i < rows; i++) {
  16.             mat.at(i).resize(columns);
  17.         }
  18.         for(int i = 0; i < rows; i++) {
  19.             for(int j = 0; j < columns; j++) {
  20.                 mat.at(i).at(j) = '#';
  21.             }
  22.         }
  23.     }
  24.     int get_rows() const {
  25.         return rows;
  26.     }
  27.     int get_columns() const {
  28.         return columns;
  29.     }
  30.     void set_value(int i, int j, char new_value) {
  31.         mat.at(i).at(j) = new_value;
  32.     }
  33.     void print() {
  34.         for(int i = 0; i < rows; i++) {
  35.             for(int j = 0; j < columns; j++) {
  36.                 std::cout << mat.at(i).at(j) ;
  37.             }
  38.             std::cout << std::endl;
  39.         }
  40.     }
  41.     char get_position(int i, int j) {
  42.         return mat.at(i).at(j);
  43.     }
  44. };
  45. class RandomMaze {
  46. private:
  47.     Maze maze;
  48.     std::vector<std::vector<bool> > visited;
  49.     std::vector<int> di{-1, 1, 0, 0};
  50.     std::vector<int> dj{0, 0, -1, 1};
  51. public:
  52.     RandomMaze(Maze _maze) {
  53.         maze = _maze;
  54.         visited.resize(maze.get_rows());
  55.         for(int j = 0; j < maze.get_rows(); j++) {
  56.             visited.at(j).resize(maze.get_columns(), false);
  57.         }
  58.     }
  59.     void generate_random_maze(int ii, int jj) {
  60.         visited[ii][jj] = true;
  61.         std::vector<std::pair<int, int> > v;
  62.         for(int i = 0; i < 4; i++) {
  63.             int ti = di.at(i) + ii;
  64.             int tj = dj.at(i) + jj;
  65.             if(ti >= 0 and ti < maze.get_rows() and tj >= 0 and tj < maze.get_columns() and !visited.at(ti).at(tj)) {
  66.                 v.push_back(std::make_pair(ti, tj));
  67.             }
  68.         }
  69.         std::random_shuffle(v.begin(), v.end());
  70.         for(std::pair<int, int> p : v) {
  71.             maze.set_value(p.first, p.second, '.');
  72.             generate_random_maze(p.first, p.second);
  73.         }
  74.         visited[ii][jj] = false;
  75.        
  76.     }
  77.     Maze get_maze() {
  78.         return maze;
  79.     }
  80.    
  81. };
  82. int main(int argc, char *argv[])
  83. {
  84.     int seed = -1;
  85.     int n, m;
  86.     if(argc > 2) {
  87.         n = atoi(argv[1]);
  88.         m = atoi(argv[2]);
  89.     }
  90.         if(argc == 3) {
  91.         seed = atoi(argv[3]);
  92.     }
  93.     if(seed == -1) {
  94.         srand(time(0));
  95.     }
  96.     else {
  97.         srand(seed);
  98.     }
  99.     n = 5;
  100.     m = 5;
  101.     Maze maze(n, m);
  102.     RandomMaze random_maze(maze);
  103.     random_maze.generate_random_maze(0, 0);
  104.     random_maze.get_maze().print();
  105.    
  106.    
  107.     return 0;
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement