Guest User

Untitled

a guest
Aug 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 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.     cout<<m[curRow][curCol];
  40.    
  41.     if(m[curRow][curCol] =='#' || curCol < 0 || curRow <1 || (curRow> (SIZE-2)) )
  42.         return false;
  43.  
  44.     for(int i=0;i<2;i++)
  45.         if( curRow==exitrow[i] && curCol == SIZE-1){
  46.             m[curRow][curCol]='#';
  47.             curCol=col;
  48.             curRow=row;
  49.             findPath(m,row,col,exitrow,curCol,curRow,s);
  50.             return true;
  51.         }
  52.  
  53.  
  54.         if(findPath(m,row,col,exitrow,curCol+1,curRow,s) && s[curRow][curCol+1]==false)
  55.             //east
  56.             return true;
  57.        
  58.         if(findPath(m,row,col,exitrow,curCol,curRow+1,s) && s[curRow+1][curCol]==false)
  59.             return true;//south
  60.        
  61.         if(findPath(m,row,col,exitrow,curCol,curRow-1,s)&& s[curRow-1][curCol]==false)
  62.             return true;//north
  63.         if(findPath(m,row,col,exitrow,curCol-1,curRow,s)&& s[curRow][curCol-1]==false)
  64.             return true;//west
  65.    
  66.         return false;
  67.  
  68. }
  69.  
  70. void generate_all_paths(char m[SIZE][SIZE],bool s[SIZE][SIZE])
  71. {   int startrow=findStart(m);
  72.     cout<<"start row "<<startrow<<endl;
  73.     int* exitRows=findExits(m);
  74.    
  75.         for(int i=0; i< sizeof(exitRows)/sizeof(int);i++)
  76.         cout<<"exit row "<<exitRows[i]<<" "<<endl;
  77.         findPath(m,startrow,0,exitRows,0,startrow,s);
  78. }
  79.  
  80. int main(){
  81.     //actual maze
  82.     char m[SIZE][SIZE];
  83.     //marking maze
  84.     bool sign[SIZE][SIZE];
  85.     for(int i=0; i<SIZE;i++)
  86.         for(int j=0; j<SIZE;j++)
  87.             sign[i][j]=false;
  88.  
  89.     ifstream maze("maze.txt");
  90.     string line;
  91.     for(int i=0; i<SIZE;i++){
  92.         getline(maze, line);
  93.         for(int j=0; j<SIZE;j++){
  94.             m[i][j]=line[j];
  95.         }
  96.     }
  97.    
  98.    
  99.     for(int i=0; i<SIZE;i++){
  100.        
  101.         for(int j=0; j<SIZE;j++){
  102.             cout<<m[i][j];
  103.         }
  104.         cout<<endl;
  105.     }
  106.    
  107.     generate_all_paths(m,sign);
  108.  
  109.     return 0;
  110. }
Add Comment
Please, Sign In to add comment