Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


backtracking maze solver in c


backtracking maze solver in c  using -'c,maze,recursive-backtracking'

I am writing the find path solution for maze [30][30]
I used backtracking in path find function;
Here is pseudo code:

bool find_path(myMap,myVisited,myPath,v,h)


find starting point [v,h] then run find_path function for that point
mark the room as visited
terminate condition 1: if h=29, mark on path then return true
recursion part: search for 8 neighbors: west,northwest,north,northeast,east,southeast, south,southwest.
check if the room is accessible, then call find_path function, if it return true, mark on find path.
terminate condition 2: return false.


When I run it, it always give segmentation fault. I had tried different method but all give same errors?
the output should be like this image:
http://postimg.org/image/cd8unwet5/
here is my code:

// myMap ='.' is a block on map
//myVisited ='*' room visited
bool isSafe(char myMap[30][30], char myVisited[30][30], int v,int h){
if(v>=0||v<30||h>=0||h<30||myMap[v][h]!='.'||myVisited[v][h]=='*')return true;
    return false;}

//3 map
//myMap contain the maze
//myVisited use to mark room visited
//myPath is final path.
bool find_path(char myMap[30][30],char myVisited[30][30],char myPath[30][30], int v,int h){
//giving h=-1 and v=-1 , find starting point.
    if(h==-1&&v==-1){
        h=h+1;
        for (v=0;v<30;v++){
        if(myMap[v][h]!='.'&& h==0) {find_path(myMap,myVisited,myPath,v,h);}
        }

    }

    myVisited[v][h]='*';    //mark room as visited.
    if(h==29){              //stop when column is 29 and mark on path
        myPath[v][h]='*';
        return true;}

    if(isSafe(myMap,myVisited,v,h-1)==true){                //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v,h-1)==true){  // there is way out
        myPath[v][h]='*';           //mark on myPath
        }
    }
    //.......same code for other room

    if(isSafe(myMap,myVisited,v,h+1)==true){                //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v,h+1)==true){  // there is way out
        myPath[v][h]='*';           //mark on myPath
        }
    }   
    if(isSafe(myMap,myVisited,v-1,h)==true){                //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v-1,h)==true){  // there is way out
        myPath[v][h]='*';           //mark on myPath
        }
    }
    if(isSafe(myMap,myVisited,v+1,h)==true){                //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v+1,h)==true){  // there is way out
        myPath[v][h]='*';           //mark on myPath
        }
    }
    if(isSafe(myMap,myVisited,v+1,h-1)==true){              //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v,h-1)==true){  // there is way out
        myPath[v][h]='*';           //mark on myPath
        }
    }
        if(isSafe(myMap,myVisited,v-1,h-1)==true){              //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v-1,h-1)==true){    // there is way out
        myPath[v][h]='*';           //mark on myPath
        }
    }
            if(isSafe(myMap,myVisited,v-1,h+1)==true){              //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v-1,h+1)==true){    // there is way out
        myPath[v][h]='*';           //mark on myPath
        }
    }
    myVisited[v][h]='.';
        return false;//back track
return false;}

    

asked Sep 9, 2015 by HilY56
0 votes
6 views



Related Hot Questions



Government Jobs Opening


...