Помогите написать код на языке C++
Создать программу, отыскивающую проход по лабиринту. Лабиринт представляется в виде матрицы, состоящей из квадратов. Каждый квадрат либо открыт, либо закрыт. Вход в закрытый квадрат запрещен. Если квадрат открыт, то вход в него возможен со стороны, но не с угла. Каждый квадрат определяется его координатами в матрице. Программа находит проход через лабиринт, двигаясь от заданного входа. После отыскания прохода программа выводит найденный путь в виде координат квадратов. Для хранения пути использовать стек.
По дате
По Рейтингу
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
// Define a struct to represent the coordinates of a cell in the maze
struct Coord {
int row;
int col;
};
// Define a struct to represent a cell in the maze
struct Cell {
bool visited = false;
bool isOpen = false;
};
// Define a function to check if a cell is valid (within the bounds of the maze and open)
bool isValidCell(const vector<vector<Cell>>& maze, const Coord& coord) {
int numRows = maze.size();
int numCols = maze[0].size();
return (coord.row >= 0 && coord.row < numRows && coord.col >= 0 && coord.col < numCols && maze[coord.row][coord.col].isOpen);
}
// Define a function to get the neighbors of a cell
vector<Coord> getNeighbors(const vector<vector<Cell>>& maze, const Coord& coord) {
vector<Coord> neighbors;
Coord up = { coord.row - 1, coord.col };
Coord down = { coord.row + 1, coord.col };
Coord left = { coord.row, coord.col - 1 };
Coord right = { coord.row, coord.col + 1 };
if (isValidCell(maze, up)) {
neighbors.push_back(up);
}
if (isValidCell(maze, down)) {
neighbors.push_back(down);
}
if (isValidCell(maze, left)) {
neighbors.push_back(left);
}
if (isValidCell(maze, right)) {
neighbors.push_back(right);
}
return neighbors;
}
// Define a function to find the path through the maze
vector<Coord> findPath(const vector<vector<Cell>>& maze, const Coord& start, const Coord& end) {
// Initialize the stack with the starting cell
stack<Coord> stack;
stack.push(start);
// Initialize a vector to store the path
vector<Coord> path;
// Mark the starting cell as visited
maze[start.row][start.col].visited = true;
while (!stack.empty()) {
// Get the top cell from the stack
Coord curr = stack.top();
// If the current cell is the end cell, return the path
if (curr.row == end.row && curr.col == end.col) {
while (!stack.empty()) {
path.push_back(stack.top());
stack.pop();
}
return path;
}
// Get the neighbors of the current cell
vector<Coord> neighbors = getNeighbors(maze, curr);
// If the current cell has unvisited neighbors, push them onto the stack
if (!neighbors.empty()) {
for (const auto& neighbor : neighbors) {
if (!maze[neighbor.row][neighbor.col].visited) {
maze[neighbor.row][neighbor.col].visited = true;
stack.push(neighbor);
}
}
}
else {
// If the current cell has no unvisited neighbors, pop it off the stack
stack.pop();
}
}
// If no path was found, return an empty vector
return path;
}
// Define a function to print the path
void printPath(const vector<Coord>& path) {
cout << "Path: ";
for (const auto& coord : path) {
cout << "(" << coord.row << ", " << coord.col << ") ";
}
cout << endl;
}
int main() {
// Define the maze
}
И в чём проблема?
Содаёте двойной булевый массив. И состояния его переменных открыто/закрыто.
А затем программу, которая будет искать путь.
Больше по теме