#include <bits/stdc++.h> using namespace std; vector<vector<char>> generateGrid(int n,…
- User
#include <bits/stdc++.h>
using namespace std;
vector<vector> generateGrid(int n, int m) { vector<vector> grid(n, vector(m, '.'));
int num_cells = n *m; int s_row, s_col, t_row, t_col; do { // Choose a random cell for S s_row = rand() % n; s_col = rand() % m; // Choose a random cell for T within a certain radius of S int t_offset = rand() % 4 + 2; int t_distance = 2 * t_offset; vector<pair<int, int>> t_positions; for (int i = -t_offset; i <= t_offset; i++) { for (int j = -t_offset; j <= t_offset; j++) { if (abs(i) + abs(j) == t_distance) { int t_row = s_row + i; int t_col = s_col + j; if (t_row >= 0 && t_row < n && t_col >= 0 && t_col < m && grid[t_row][t_col] == '.' && (t_row != s_row || t_col != s_col)) { t_positions.emplace_back(t_row, t_col); } } } } // If there are no empty cells within the radius, try again if (t_positions.empty()) { continue; } // Choose a random cell from the available options int t_index = rand() % t_positions.size(); t_row = t_positions[t_index].first; t_col = t_positions[t_index].second; // Check if S and T are adjacent if (abs(t_row - s_row) <= 1 && abs(t_col - s_col) <= 1) { // 30% chance to keep S and T together if (rand() % 100 < 30) { break; } else { continue; } } // Mark S and T cells grid[s_row][s_col] = 'S'; grid[t_row][t_col] = 'T'; break; } while (true); // Add other S and T cells int num_s_t = rand() % (num_cells - num_obstacles - 2) + 2; for (int i = 2; i < num_s_t; i++) { int row, col; do { row = rand() % n; col = rand() % m; } while (grid[row][col] != '.' || abs(row - s_row) + abs(col - s_col) < 4 || abs(row - t_row) + abs(col - t_col) < 4); if (i % 2 == 0) { grid[row][col] = 'S'; } else { grid[row][col] = 'T'; } } return grid;}
int main(int argc, char *argv[]) { srand(atoi(argv[1]) + atoi(argv[2]) + atoi(argv[3]) + atoi(argv[4]));
int min_n = 2; int min_m = 2; int max_n = atoi(argv[3]); int max_m = atoi(argv[4]); int max_q = atoi(argv[2]); int q = 1 + rand() % max_q; cout<<q<<endl; for (int i = 0; i < q; i++) { int n = min_n + rand() % (max_n - min_n + 1); int m = min_m + rand() % (max_m - min_m + 1); if (n == 1 && m == 1) { n = 2; }
cout << n << " " << m << endl; vector<vector<char>> grid = generateGrid(n,m); if (grid[n - 1][m - 1] != '.') { grid[n - 1][m - 1] = '.'; } for (int j = 0; j < n; j++) { for (int k = 0; k < m; k++) { cout << grid[j][k]; } cout << endl; }}
return 0; } Измени код так чтобы он не циклился
- Assistant
В коде уже есть проверка на наличие пустых ячеек в нужном радиусе вокруг S для выбора позиции T. Однако, если таких позиций не оказывается, текущий код продолжает бесконечно перебирать случайные позиции для S и T. Чтобы избежать этого цикла, можно добавить счётчик попыток и ограничить его определенным количеством, например:
int max_attempts = 100; // Максимальное число попыток для нахождения позиции T int attempts = 0; // Счётчик попыток
// ...
do { // Выбор позиции S // ...
// Выбор позиции T в нужном радиусе вокруг S // с проверкой на наличие пустых ячеек // ... attempts++; // Если превышено максимальное число попыток, // прерываем цикл и генерируем новый случайный лабиринт if (attempts >= max_attempts) { break; }} while (true);
- User
Выведи полный код
- Assistant
- User
Измени только вот этот код vector<vector> generateGrid(int n, int m) { vector<vector> grid(n, vector(m, '.'));
int num_cells = n *m; int s_row, s_col, t_row, t_col; do { // Choose a random cell for S s_row = rand() % n; s_col = rand() % m; // Choose a random cell for T within a certain radius of S int t_offset = rand() % 4 + 2; int t_distance = 2 * t_offset; vector<pair<int, int>> t_positions; for (int i = -t_offset; i <= t_offset; i++) { for (int j = -t_offset; j <= t_offset; j++) { if (abs(i) + abs(j) == t_distance) { int t_row = s_row + i; int t_col = s_col + j; if (t_row >= 0 && t_row < n && t_col >= 0 && t_col < m && grid[t_row][t_col] == '.' && (t_row != s_row || t_col != s_col)) { t_positions.emplace_back(t_row, t_col); } } } } // If there are no empty cells within the radius, try again if (t_positions.empty()) { continue; } // Choose a random cell from the available options int t_index = rand() % t_positions.size(); t_row = t_positions[t_index].first; t_col = t_positions[t_index].second; // Mark S and T cells grid[s_row][s_col] = 'S'; grid[t_row][t_col] = 'T'; break; } while (true); // Add other S and T cells int num_s_t = rand() % (num_cells/4) + 2; for (int i = 2; i < num_s_t; i++) { int row, col; do { row = rand() % n; col = rand() % m; } while (grid[row][col] != '.' || abs(row - s_row) + abs(col - s_col) < 4 || abs(row - t_row) + abs(col - t_col) < 4); if (i % 2 == 0) { grid[row][col] = 'S'; } else { grid[row][col] = 'T'; } } return grid; - Assistant
Замени код функции
generateGrid()на:vector<vector<char>> generateGrid(int n, int m) { vector<vector<char>> grid(n, vector<char>(m, '.')); int num_cells = n *m; int s_row, s_col, t_row, t_col; do { // Choose a random cell for S s_row = rand() % n; s_col = rand() % m; // Choose a random cell for T within a certain radius of S int t_offset = rand() % 4 + 2; int t_distance = 2 * t_offset; vector<pair<int, int>> t_positions; for (int i = -t_offset; i <= t_offset; i++) { for (int j = -t_offset; j <= t_offset; j++) { if (abs(i) + abs(j) == t_distance) { int t_row = s_row + i; int t_col = s_col + j; if (t_row >= 0 && t_row < n && t_col >= 0 && t_col < m && grid[t_row][t_col] == '.' && (t_row != s_row || t_col != s_col)) { t_positions.emplace_back(t_row, t_col); } } } } // If there are no empty cells within the radius, try again if (t_positions.empty()) { continue; } // Choose a random cell from the available options int t_index = rand() % t_positions.size(); t_row = t_positions[t_index].first; t_col = t_positions[t_index].second; // Mark S and T cells grid[s_row][s_col] = 'S'; grid[t_row][t_col] = 'T'; break; } while (true); // Add other S and T cells int num_s_t = rand() % (num_cells/4 - 2) + 2; // Изменила эту строку for (int i = 2; i < num_s_t; i++) { int row, col; do { row = rand() % n; col = rand() % m; } while (grid[row][col] != '.' || abs(row - s_row) + abs(col - s_col) < 4 || abs(row - t_row) + abs(col - t_col) < 4); if (i % 2 == 0) { grid[row][col] = 'S'; } else { grid[row][col] = 'T'; } } return grid; }