Advertisement
RoshHoul

lab

Jan 27th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. vector<pair<int, int> > checkAdj(string** maze, int x, int y, int rows, int cols, pair<int,int> prev) {
  8.  
  9.     vector<pair<int, int> > tempCoord;
  10.  
  11.  
  12.     auto left = make_pair(x, y-1);
  13.     auto right = make_pair(x, y+1);
  14.     auto up = make_pair(x-1, y);
  15.     auto down = make_pair(x+1, y);
  16.  
  17.     if (y > 0 && maze[x][y - 1] == "." && (left != prev))
  18.         tempCoord.push_back(left);
  19.  
  20.     if (x > 0 && maze[x - 1][y] == "." && (up != prev))
  21.         tempCoord.push_back(up);
  22.  
  23.     if (x + 1 < rows && maze[x + 1][y] == "." && (down != prev))
  24.         tempCoord.push_back(down);
  25.  
  26.     if (y + 1 < cols && maze[x][y + 1] == "." && (right != prev))
  27.         tempCoord.push_back(right);
  28.  
  29.  
  30.     return tempCoord;
  31. }
  32.  
  33. int findPath(string** maze, int x, int y, int rows, int cols, int prevX, int prevY, int count) {
  34.     auto prev = make_pair(prevX, prevY);
  35.     auto directions = checkAdj(maze, x, y, rows, cols, prev);
  36.  
  37.     if ( (prevX == -1) && (prevY == -1)) {
  38.         if (directions.size() < 1) {
  39.             return count;
  40.         }
  41.     } else if (directions.size() < 1 && (prevX != -1) && (prevY != -1)) {
  42.         return count;
  43.     }
  44.  
  45.  
  46.     if (directions.size() >= 1) {
  47.         int finalMax = 0;
  48.         for (int i = 0; i < directions.size(); i++) {
  49.             int currentMaxPath = 0;
  50.             currentMaxPath = findPath(maze, directions[i].first, directions[i].second, rows, cols, x, y, count + 1);
  51.             finalMax = currentMaxPath > finalMax ? currentMaxPath : finalMax;
  52.         }
  53.  
  54.         return finalMax;
  55.     }
  56.  
  57.  
  58. }
  59.  
  60. int main(int argc, char** argv) {
  61.  
  62.     int rows, cols;
  63.  
  64.     vector <pair<int, int> > marked;
  65.  
  66.     cout << "Enter maze dimensions" << endl;
  67.     cin >> rows >> cols;
  68.    
  69.     string** maze = new string*[rows];
  70.     for (int i = 0; i < cols; i++) {
  71.         maze[i] = new string[cols];
  72.     }
  73.  
  74.     cout << "enter maze" << endl;
  75.     for (int i = 0; i < rows; i++) {
  76.         for (int j = 0; j < cols; j++) {
  77.             char x;
  78.             cin >> x;
  79.             maze[i][j] += x;
  80.         }
  81.     }
  82.  
  83.     int result = 0;
  84.  
  85.     for (int i = 0; i < rows; i++) {
  86.         bool startFound = false;
  87.         for (int j = 0; j < cols; j++) {
  88.             if (maze[i][j] == ".") {
  89.                 result = findPath(maze, i, j, rows, cols, -1, -1, 1);
  90.                 startFound = true;
  91.                 break;
  92.             }
  93.         }
  94.         if (startFound)
  95.             break;
  96.     }
  97.     cout << result << endl;
  98.  
  99.     for (int i = 0; i < rows; i++) {
  100.         delete[] maze[i];
  101.     }
  102.  
  103.     delete[] maze;
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement