Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- void findStartCoordinates(int start_x, int start_y,
- std::vector<std::vector<char>> maze) {
- for (int i = 0; i < maze.size(); i++) {
- for (int j = 0; j < maze[0].size(); j++) {
- if (maze[i][j] == 'A') {
- start_x = i;
- start_y = j;
- return;
- }
- }
- }
- }
- bool solveMaze(int x, int y, std::vector<std::vector<char>>& maze) {
- if (maze[x][y] == 'B') return true;
- if (x < 0 || x >= maze.size() ||
- y < 0 || y >= maze.size() ||
- maze[x][y] != ' ') return false;
- maze[x][y] = '.';
- if (solveMaze(x, y - 1, maze)) return true;
- if (solveMaze(x + 1, y, maze)) return true;
- if (solveMaze(x, y + 1, maze)) return true;
- if (solveMaze(x - 1, y, maze)) return true;
- maze[x][y] = ' ';
- return false;
- }
- void printMaze(std::vector<std::vector<char>> maze) {
- for (auto line : maze) {
- for (auto obs : line) {
- std::cout << obs;
- }
- std::cout << '\n';
- }
- }
- int main() {
- std::ifstream file("labirynt.txt");
- std::vector<std::vector<char>> maze;
- for (std::string line;
- std::getline(file, line);
- maze.emplace_back(line.begin(), line.end()));
- int start_x, start_y;
- findStartCoordinates(start_x, start_y, maze);
- maze[start_x][start_y] = ' ';
- solveMaze(start_x, start_y, maze);
- maze[start_x][start_y] = 'A';
- printMaze(maze);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement