Guest User

Untitled

a guest
Aug 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. // mazeSolver.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. using namespace std;
  9. const int SIZE=5;
  10.  
  11.  
  12. int findStart(char m[SIZE][SIZE])
  13. {
  14.     int col=0;
  15.     int row=0;
  16.     for(row=0; row <SIZE;row++)
  17.         if(m[row][col] == 's' || m[row][col] == 'S')
  18.             return row;
  19.     return 0;
  20. }
  21.  
  22. int* findExits(char m[SIZE][SIZE])
  23. {   int* exits=new int[SIZE];
  24.     int col=0;
  25.     int row=0;
  26.     int i=0;
  27.     for(row=0; row <SIZE;row++)
  28.         if(m[row][SIZE-1] !='#'){
  29.             exits[i]=row;
  30.            
  31.             i++;
  32.         }
  33.     return exits;
  34. }
  35.  
  36. bool findPath(char m[SIZE][SIZE],int row,int col,int* exitrow,int curCol,int curRow,bool s[SIZE][SIZE])
  37. {  
  38.     s[curRow][curCol]=true;
  39.    
  40.    
  41.    
  42.     if(m[curRow][curCol] =='#' || curCol < 0 || curRow <1 || (curRow> (SIZE-2)) )
  43.         return false;
  44.     cout<<m[curRow][curCol];
  45.  
  46.     for(int i=0;i<2;i++)
  47.         if( curRow==exitrow[i] && curCol == SIZE-1){
  48.             cout<<endl;
  49.             m[curRow][curCol]='#';
  50.             curCol=col;
  51.             curRow=row;
  52.             findPath(m,row,col,exitrow,curCol,curRow,s);
  53.             for(int i=0; i<SIZE;i++)
  54.                 for(int j=0; j<SIZE;j++)
  55.                     s[i][j]=false;
  56.             return true;
  57.         }
  58.  
  59.  
  60.         if(findPath(m,row,col,exitrow,curCol+1,curRow,s) && s[curRow][curCol]==false)
  61.             //east
  62.             return true;
  63.        
  64.         if(findPath(m,row,col,exitrow,curCol,curRow+1,s) && s[curRow][curCol]==false)
  65.             return true;//south
  66.        
  67.         if(findPath(m,row,col,exitrow,curCol,curRow-1,s)&& s[curRow][curCol]==false)
  68.             return true;//north
  69.         if(findPath(m,row,col,exitrow,curCol-1,curRow,s)&& s[curRow][curCol]==false)
  70.             return true;//west
  71.    
  72.         return false;
  73.  
  74. }
  75.  
  76. void generate_all_paths(char m[SIZE][SIZE],bool s[SIZE][SIZE])
  77. {   int startrow=findStart(m);
  78.     cout<<"start row "<<startrow<<endl;
  79.     int* exitRows=findExits(m);
  80.    
  81.         for(int i=0; i< sizeof(exitRows)/sizeof(int);i++)
  82.         cout<<"exit row "<<exitRows[i]<<" "<<endl;
  83.         findPath(m,startrow,0,exitRows,0,startrow,s);
  84. }
  85.  
  86. int main(){
  87.     //actual maze
  88.     char m[SIZE][SIZE];
  89.     //marking maze
  90.     bool sign[SIZE][SIZE];
  91.     for(int i=0; i<SIZE;i++)
  92.         for(int j=0; j<SIZE;j++)
  93.             sign[i][j]=false;
  94.  
  95.     ifstream maze("maze.txt");
  96.     string line;
  97.     for(int i=0; i<SIZE;i++){
  98.         getline(maze, line);
  99.         for(int j=0; j<SIZE;j++){
  100.             m[i][j]=line[j];
  101.         }
  102.     }
  103.    
  104.    
  105.     for(int i=0; i<SIZE;i++){
  106.        
  107.         for(int j=0; j<SIZE;j++){
  108.             cout<<m[i][j];
  109.         }
  110.         cout<<endl;
  111.     }
  112.    
  113.     generate_all_paths(m,sign);
  114.  
  115.     return 0;
  116. }
Add Comment
Please, Sign In to add comment