Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<pair<int, int> > checkAdj(string** maze, int x, int y, int rows, int cols, pair<int,int> prev) {
- vector<pair<int, int> > tempCoord;
- auto left = make_pair(x, y-1);
- auto right = make_pair(x, y+1);
- auto up = make_pair(x-1, y);
- auto down = make_pair(x+1, y);
- if (y > 0 && maze[x][y - 1] == "." && (left != prev))
- tempCoord.push_back(left);
- if (x > 0 && maze[x - 1][y] == "." && (up != prev))
- tempCoord.push_back(up);
- if (x + 1 < rows && maze[x + 1][y] == "." && (down != prev))
- tempCoord.push_back(down);
- if (y + 1 < cols && maze[x][y + 1] == "." && (right != prev))
- tempCoord.push_back(right);
- return tempCoord;
- }
- int findPath(string** maze, int x, int y, int rows, int cols, int prevX, int prevY, int count) {
- auto prev = make_pair(prevX, prevY);
- auto directions = checkAdj(maze, x, y, rows, cols, prev);
- if ( (prevX == -1) && (prevY == -1)) {
- if (directions.size() < 1) {
- return count;
- }
- } else if (directions.size() < 1 && (prevX != -1) && (prevY != -1)) {
- return count;
- }
- if (directions.size() >= 1) {
- int finalMax = 0;
- for (int i = 0; i < directions.size(); i++) {
- int currentMaxPath = 0;
- currentMaxPath = findPath(maze, directions[i].first, directions[i].second, rows, cols, x, y, count + 1);
- finalMax = currentMaxPath > finalMax ? currentMaxPath : finalMax;
- }
- return finalMax;
- }
- }
- int main(int argc, char** argv) {
- int rows, cols;
- vector <pair<int, int> > marked;
- cout << "Enter maze dimensions" << endl;
- cin >> rows >> cols;
- string** maze = new string*[rows];
- for (int i = 0; i < cols; i++) {
- maze[i] = new string[cols];
- }
- cout << "enter maze" << endl;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- char x;
- cin >> x;
- maze[i][j] += x;
- }
- }
- int result = 0;
- for (int i = 0; i < rows; i++) {
- bool startFound = false;
- for (int j = 0; j < cols; j++) {
- if (maze[i][j] == ".") {
- result = findPath(maze, i, j, rows, cols, -1, -1, 1);
- startFound = true;
- break;
- }
- }
- if (startFound)
- break;
- }
- cout << result << endl;
- for (int i = 0; i < rows; i++) {
- delete[] maze[i];
- }
- delete[] maze;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement