Hello!
I am trying to write a program using recursive functions to identify the path to a given maze. The thing that I am confused about is how to move to the next direction in order to check which way the path goes. For example:
000000
011100
010100
010110
0S00F0
000000
With this maze if I start at S, then I would have to check each side (left, right, up, and down) in order to see where the path continues. S-start F-finish 1-path 0-blocked.
Here is the code I have so far:
#include <iostream>
#include <fstream>
using namespace std;
enum Direction{N, S, E, W, failed};
Direction directionCheck;
bool findPath(char array[15][15],int x,int y);
int main(){
char mazeArray[15][15];
ifstream infile;
infile.open("Home\\COSC220\\Project1\\mazereadin");
for(int i=0;i<15;i++){
for(int j=0;j<15;j++){
infile>>mazeArray[i][j];
}
}
bool path;
path=findPath(mazeArray,13,1);
if(path==true){
for(int k=0;k<15;k++){
for(int a=0;a<15;a++){
cout<<mazeArray[k][a]<<" ";
}
cout<<endl;
}
}
infile.close();
return 0;
}
bool findPath(char array[15][15],int x,int y){
bool found;
if(array[x][y]=='F')
return true;
else if(array[x][y]=='C' || array[x][y]=='X')
return false;
array[x][y]='X';
if(array[x][y]=='O' || array[x][y]=='S'){
if(array[x][y]!='C'){
directionCheck=N;
found=false;
}
while(found==false && directionCheck!=failed){
if(directionCheck==N)
found=findPath(array,x,y-1);
else if(directionCheck==E)
found=findPath(array,x+1,y);
else if(directionCheck==S)
found=findPath(array,x,y+1);
else if(directionCheck==W)
found=findPath(array,x-1,y);
directionCheck++;
}
if(found==false){
array[x][y]='O';
return false;
}
}
}
Thanks for all your help!