Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <stack>
- class Maze {
- private:
- int rows, columns;
- std::vector<std::vector<char> > mat;
- public:
- Maze(int _rows = 2, int _columns = 2) {
- rows = _rows;
- columns = _columns;
- mat.resize(rows);
- for(int i = 0; i < rows; i++) {
- mat.at(i).resize(columns);
- }
- for(int i = 0; i < rows; i++) {
- for(int j = 0; j < columns; j++) {
- mat.at(i).at(j) = '#';
- }
- }
- }
- int get_rows() const {
- return rows;
- }
- int get_columns() const {
- return columns;
- }
- void set_value(int i, int j, char new_value) {
- mat.at(i).at(j) = new_value;
- }
- void print() {
- for(int i = 0; i < rows; i++) {
- for(int j = 0; j < columns; j++) {
- std::cout << mat.at(i).at(j) ;
- }
- std::cout << std::endl;
- }
- }
- char get_position(int i, int j) {
- return mat.at(i).at(j);
- }
- };
- class RandomMaze {
- private:
- Maze maze;
- std::vector<std::vector<bool> > visited;
- std::vector<int> di{-1, 1, 0, 0};
- std::vector<int> dj{0, 0, -1, 1};
- public:
- RandomMaze(Maze _maze) {
- maze = _maze;
- visited.resize(maze.get_rows());
- for(int j = 0; j < maze.get_rows(); j++) {
- visited.at(j).resize(maze.get_columns(), false);
- }
- }
- void generate_random_maze(int ii, int jj) {
- visited[ii][jj] = true;
- std::vector<std::pair<int, int> > v;
- for(int i = 0; i < 4; i++) {
- int ti = di.at(i) + ii;
- int tj = dj.at(i) + jj;
- if(ti >= 0 and ti < maze.get_rows() and tj >= 0 and tj < maze.get_columns() and !visited.at(ti).at(tj)) {
- v.push_back(std::make_pair(ti, tj));
- }
- }
- std::random_shuffle(v.begin(), v.end());
- for(std::pair<int, int> p : v) {
- maze.set_value(p.first, p.second, '.');
- generate_random_maze(p.first, p.second);
- }
- visited[ii][jj] = false;
- }
- Maze get_maze() {
- return maze;
- }
- };
- int main(int argc, char *argv[])
- {
- int seed = -1;
- int n, m;
- if(argc > 2) {
- n = atoi(argv[1]);
- m = atoi(argv[2]);
- }
- if(argc == 3) {
- seed = atoi(argv[3]);
- }
- if(seed == -1) {
- srand(time(0));
- }
- else {
- srand(seed);
- }
- n = 5;
- m = 5;
- Maze maze(n, m);
- RandomMaze random_maze(maze);
- random_maze.generate_random_maze(0, 0);
- random_maze.get_maze().print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement