Guest User

Untitled

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