Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <random>
- #include <algorithm>
- #include <fstream>
- #include <stack>
- using namespace std;
- void gerarCaminho(vector<vector<char>>& layout, int startX, int startY) {
- // mover pra cima, baixo, esquerda e direita
- vector<pair<int, int>> directions = {{-2, 0}, {2, 0}, {0, -2}, {0, 2}};
- random_device rd;
- mt19937 g(rd());
- //criando a pilha pra não ter overflow e lidar com numeros grandes
- stack<pair<int, int>> s;
- s.push({startX, startY});
- //tive de deixar de ser recursivo para não fritar meu pc
- while (!s.empty()) {
- auto [x, y] = s.top();
- s.pop();
- // embaralhando as direções
- shuffle(directions.begin(), directions.end(), g);
- for (auto& dir : directions) {
- int nx = x + dir.first;
- int ny = y + dir.second;
- if (nx > 0 && nx < layout.size() - 1 && ny > 0 && ny < layout[0].size() - 1 && layout[nx][ny] == '|') {
- layout[nx][ny] = ' ';
- layout[x + dir.first / 2][y + dir.second / 2] = ' ';
- s.push({nx, ny});
- }
- }
- }
- }
- int main() {
- //altura e largura
- int largura = 99;
- int comprimento = 99;
- // enche de paredes
- vector<vector<char>> layout(largura, vector<char>(comprimento, '|'));
- random_device rd;
- mt19937 g(rd());
- int entrada = rand() % (comprimento - 2) + 1;
- int saida = rand() % (comprimento - 2) + 1;
- // definir o começo
- layout[0][entrada] = ' ';
- gerarCaminho(layout, 1, entrada);
- vector<int> possibilidades_saida;
- for (int i = 0; i < comprimento; i++) {
- if (layout[largura - 2][i] == ' ') {
- possibilidades_saida.push_back(i);
- }
- }
- if (!possibilidades_saida.empty()) {
- saida = possibilidades_saida[rand() % possibilidades_saida.size()];
- layout[largura - 1][saida] = ' ';
- }
- // ver o resultado
- for (int i = 0; i < largura; i++) {
- for (int j = 0; j < comprimento; j++) {
- cout << layout[i][j] << " ";
- }
- cout << endl;
- }
- // escrever num .txr
- ofstream outputFile("labirinto.txt");
- if (outputFile.is_open()) {
- for (int i = 0; i < largura; i++) {
- for (int j = 0; j < comprimento; j++) {
- outputFile << layout[i][j] << " ";
- }
- outputFile << endl;
- }
- outputFile.close();
- cout << "Labirinto.txt foi criado" << endl;
- } else {
- cout << "o criacionismo falhou." << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment